JavaFX8 print API: how to set up print area correctly

In my javafx application, I use the JavaFX 8 print API to print the node, I get a problem with the print area, despite the fact that I installed 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 the snapshot of the node I want to print: enter image description here

and this is what i get when i type node enter image description here

+4
source share
2 answers

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

You can view the fields if you print them:

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

, . Node, . Node , .

  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/

+5

EDIT ( Menai Ala Eddine): ( Node ), Node : [END_EDIT]

, . @NwDx ( , - ), prefSize Node :

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

All Articles