Search code examples
javajavafxprintingnodes

Is there a way to print multiple nodes on one paper using javaFX?


I can successfully print table on paper, but if I want to print also for example label above that table on the same paper, how to achieve that? This is my code for printing:

private void print(Node node) {
    Printer printer = Printer.getDefaultPrinter();
    PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.HARDWARE_MINIMUM);
    PrinterJob job = PrinterJob.createPrinterJob();
    double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();                
    Scale scale = new Scale(scaleX, 1);
    node.getTransforms().add(scale);

    if (job != null && job.showPrintDialog(node.getScene().getWindow())) {
        boolean success = job.printPage(pageLayout, node);
        if (success) {
            job.endJob();
        }
    }

    node.getTransforms().remove(scale);
}

I call print method in button like this:

printButton.setOnAction(e -> print(table));

Any ideas?


Solution

  • Just place your nodes into a container like a Pane and then print this pane. The pane is a Node itself.