How to print only text?

I am trying to send some text to the printer. I need only text printed, wrapped on the edge of the page and, if necessary, moving to another page.

Here is a minimal example of what I'm doing now:

@FXML
private void print() {
    TextArea printArea = new TextArea(textArea.getText());
    printArea.setWrapText(true);
    printArea.getChildrenUnmodifiable().forEach(node -> node.setStyle("-fx-background-color: transparent"));
    printArea.setStyle("-fx-background-color: transparent");

    PrinterJob printerJob = PrinterJob.createPrinterJob();

    if (printerJob != null && printerJob.showPrintDialog(textArea.getScene().getWindow())) {
        if (printerJob.printPage(printArea)) {
            printerJob.endJob();
            // done printing
        } else {
            // failed to print
        }
    } else {
        // failed to get printer job or failed to show print dialog
    }
}

As a result, printing is a gray background, which seems to be the control itself, as well as the scroll bar. Am I approaching this wrong? I feel like struggling with the API by customizing and typing a control instead of just sending text to print. A.

The example below was taken from the camera of my mobile phone, so the white paper looks a little light gray, but you can still see the gray background from the control and scroll bar.

print image example

+4
source share
1

TextArea 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");
    }
}

, MaxWidth TextFlow PrinterJob . ​​

+3

All Articles