JavaFX8 Print API:如何正确设置Printable区域

时间:2015-07-05 13:47:20

标签: printing javafx

在我的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

2 个答案:

答案 0 :(得分:8)

在您的方法中,您需要获得硬件边距。即使您将边距设置为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/

答案 1 :(得分:0)

编辑(在Menai Ala Eddine备注之后):如果要打印Region(Node的子节点以及所有控件和窗格和图表的父节点),则可以更改参数Node与地区 并使用此解决方案: [END_EDIT]

由于我无法添加任何评论,我将在此处提供解决方案。我修改了 通过使用以下代码行将节点的prefSize设置为打印页面布局大小,@ NwDx的答案(它对我不起作用,我最终得到了一些奇怪的缩放):

node.setPrefSize(pageLayout.getPrintableWidth(), pageLayout.getPrintableHeight());

整个片段现在看起来像这样:

public static void printNode(final Region region) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    Printer printer = Printer.getDefaultPrinter();
    PageLayout pageLayout
        = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.HARDWARE_MINIMUM);
    PrinterJob job = PrinterJob.createPrinterJob();

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