有没有办法使用JavaFX在一张纸上打印多个节点?

huangapple go评论70阅读模式
英文:

Is there a way to print multiple nodes on one paper using 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));

有什么想法吗?

英文:

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?

答案1

得分: 1

只需将您的节点放入类似 Pane 的容器中,然后打印此容器。该容器本身就是一个节点。

英文:

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

huangapple
  • 本文由 发表于 2020年8月20日 15:45:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63500518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定