JavaFX8 Print API:如何正确设置可打印区域 [英] JavaFX8 Print API : how to set correctly the Printable area

查看:20
本文介绍了JavaFX8 Print API:如何正确设置可打印区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my javafx application , I'm using JavaFX 8 printing API to print a node , i am getting problem of the printing area , despite i have set the pageLayout with A4 paper .... here is my code :

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

这是我要打印的节点的快照:

And here is a snapshot of the node that i want to print it is :

这是我打印节点时得到的

and here is what i am getting when i print the node

推荐答案

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

In your method you need to get the hardware able margins. Even if you set the margins to 0, your printer has a non printable margin around the sheet.

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

You can view the margins if you print them out:

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

并且您无法将边距设置为小于零的值.因此,您需要缩放要打印的节点.节点将被缩放、打印然后不缩放.

And you are not able to set the margins to a value less than zero. So you need to scale your Node that would be printed. The node will be scaled, printed and then unscaled.

  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/

Inspired by the solution found here: https://carlfx.wordpress.com/2013/07/15/introduction-by-example-javafx-8-printing/

这篇关于JavaFX8 Print API:如何正确设置可打印区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆