How do I print only text?

前端 未结 1 799
名媛妹妹
名媛妹妹 2021-01-14 00:40

I am trying to send some text to a printer. I need just the text printed, wrapped at the page margin and flowing to another page if necessary.

Here is a minimal exa

相关标签:
1条回答
  • 2021-01-14 00:48

    Instead of a TextArea, print a TextFlow:

    private void print() {
        TextFlow printArea = new TextFlow(new Text(textArea.getText()));
    
        PrinterJob printerJob = PrinterJob.createPrinterJob();
    
        if (printerJob != null && printerJob.showPrintDialog(textArea.getScene().getWindow())) {
            PageLayout pageLayout = printerJob.getJobSettings().getPageLayout();
            printArea.setMaxWidth(pageLayout.getPrintableWidth());
    
            if (printerJob.printPage(printArea)) {
                printerJob.endJob();
                // done printing
            } else {
                System.out.println("Failed to print");
            }
        } else {
            System.out.println("Canceled");
        }
    }
    

    Notice that the TextFlow's maxWidth needs to be set using the PrinterJob's page layout, after the print dialog has been shown.

    0 讨论(0)
提交回复
热议问题