gpt4 book ai didi

printing - JavaFX8 打印 API : how to set correctly the Printable area

转载 作者:行者123 更新时间:2023-12-04 14:26:25 27 4
gpt4 key购买 nike

在我的 javafx 应用程序中,我使用 JavaFX 8 打印 API 来打印节点,尽管我已经用 A4 纸设置了 pageLayout,但我遇到了打印区域的问题......这是我的代码:

public static  void printNode(final Node node) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Printer printer = Printer.getDefaultPrinter();
PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, 0,0,0,0 );
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null && job.showPrintDialog(node.getScene().getWindow()) ) {
boolean success = job.printPage(pageLayout, node);
if (success) {
job.endJob();
}
}

这是我要打印的节点的快照:
enter image description here

这是我打印节点时得到的
enter image description here

最佳答案

在您的方法中,您需要获得硬件能力余量。即使您将边距设置为 0,您的打印机在纸张周围也有不可打印的边距。

如果打印出来,您可以查看页边距:

System.out.println("PageLayout: " + pageLayout.toString());

而且您无法将边距设置为小于零的值。因此,您需要缩放将要打印的节点。该节点将被缩放、打印然后不缩放。
  public static void printNode(final Node node) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Printer printer = Printer.getDefaultPrinter();
PageLayout pageLayout
= printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.HARDWARE_MINIMUM);
PrinterAttributes attr = printer.getPrinterAttributes();
PrinterJob job = PrinterJob.createPrinterJob();
double scaleX
= pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();
double scaleY
= pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();
Scale scale = new Scale(scaleX, scaleY);
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);
}

灵感来自此处找到的解决方案: https://carlfx.wordpress.com/2013/07/15/introduction-by-example-javafx-8-printing/

关于printing - JavaFX8 打印 API : how to set correctly the Printable area,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31231021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com