首页 > 解决方案 > JavaFX 打印机页面(布局,节点)不打印 TextFlow

问题描述

我正在尝试打印格式化的文本。这是我的代码:

public class ProvaTextFlow {

    public static void main(String[] args)
    {
        ProvaTextFlow p = new ProvaTextFlow() ;
        Node tf = p.creaTesto();
        p.stampa(tf);
    } 

    public Node creaTesto( )
    {
      String s1 = "Dear Mom," + "\n"
              +   " I am going out dancing tonight with :" + "\n\n\n"  ;

      String f1 = " (girl) friend 1" + "\n" +
                  "        friend 2" + "\n" +
                  " (girl) friend 3" + "\n\n\n\n"  ;

        String closing = "don't wait for me" + "\n\n\n\n"
                + "bye" + "\n\n" ;


      Text t1 = new Text (s1); 
      Text t2 = new Text (f1); 
      Text t3 = new Text (closing) ;

      t1.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
      t2.setFont(Font.font("Tahoma", FontWeight.BOLD, FontPosture.ITALIC, 20));
      t3.setFont(Font.font("Tahoma", FontPosture.ITALIC, 20));

      TextFlow textFlow = new TextFlow() ; ;
        try {

            //Setting the width  
            textFlow.setPrefSize(2480, 3508); // 2480 X 3508 = A4(300)

            //Setting the line spacing  
            textFlow.setLineSpacing(5.0);

            //Retrieving the observable list of the TextFlow Pane 
            textFlow.getChildren().addAll(t1, t2, t3);

            // method to extract out of a TextFlow the String text.
            //             
            //////////////////////            
            //            StringBuilder sb = new StringBuilder();
            //            for (Node node : textFlow.getChildren()) 
            //            {
            //             if (node instanceof Text) 
            //             {
            //               sb.append(((Text) node).getText());
            //             }
            //            }
            //            String fullText = sb.toString();
            /////////////////////////            
            //            System.out.println("fulltext => " + fullText); 
        // this (when uncommented) correctly prints the unformatted text      } catch (Exception e) {
            System.out.println(e);
        }
        return textFlow;
    }

    public void stampa (Node n2p)
    {
        Printer printer = Printer.getDefaultPrinter(); 

        javafx.print.PageLayout pageLayout = printer.createPageLayout
                                            (
                                              Paper.A4, 
                                              PageOrientation.PORTRAIT, 
                                              Printer.MarginType.DEFAULT
                                            ); 

        PrinterJob job = PrinterJob.createPrinterJob();
        boolean result = job.printPage(pageLayout, n2p);
        System.out.println(" print result => " + result);// this gives true

        job.endJob();
    }
}

此代码编译运行正确,job.printPage()返回true,打印机开始打印,但 A4 纸完全空白。基本上,我构建了一个TextFlow,其中三个字符串以不同的方式格式化,然后我将这个 ( TextFlow)传递Node给该Stampa方法。如果我打印 a TableView,则完全没有问题。我错过了什么吗?谢谢

标签: javafxprintingtextflow

解决方案


推荐阅读