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
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.