I am attempting to send some potentially long text to a printer for... well, printing. Here is a minimal example that demonstrates how I am currently printing text:
@FXML
private void print() {
Text printText = new Text(textArea.getText());
TextFlow printArea = new TextFlow(printText);
printArea.setTextAlignment(TextAlignment.LEFT);
printArea.setMaxHeight(Region.USE_COMPUTED_SIZE);
PrinterJob printerJob = PrinterJob.createPrinterJob();
if (printerJob != null && printerJob.showPrintDialog(textArea.getScene().getWindow())) {
PageLayout pageLayout = printerJob.getJobSettings().getPageLayout();
printArea.setMaxWidth(pageLayout.getPrintableWidth());
if (printerJob.printPage(pageLayout, printArea)) {
printerJob.endJob();
// done printing
} else {
System.err.println("Printing failed!");
}
} else {
System.err.println("Unable to create printer job or printer dialog cancelled by user");
}
}
The code above prints text as expected, except that no matter how much text is being printed, it always prints just a single page. In the print dialog that is shown, under the section for "Print range", I have been selecting the "All" option. I have tried choosing the "Pages" option (which for some reason has been defaulting from 1 to 9999 - this is a little odd since the text should be at most two pages long), but have not had any success in printing more than a single page. I have also tried manually setting the page range on the JobSettings
object, but that didn't seem to do anything either.
How can I use the PrinterJob
and its related classes to get a long piece of text to properly print on multiple pages?