有没有一种方法可以使用javaFX在一张纸上打印多个节点?

问题描述

我可以在纸上成功打印表格,但是例如,如果我还要在同一张纸上的表格上方打印label,该如何实现? 这是我的打印代码

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

我在按钮中这样调用print方法

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

有什么想法吗?

解决方法

只需将节点放入“窗格”之类的容器中,然后打印此窗格。窗格本身就是一个节点。