2

In JavaFX, I want to print out a photo to 10x15 paper. There are some Paper constansts, but there is no 100x150 mm constant.

Is it possible to create an own Paper to use it in PageLayout?

Thanks.

PageLayout pageLayout = printer.createPageLayout(Paper.JAPANESE_POSTCARD, PageOrientation.LANDSCAPE, Printer.MarginType.EQUAL);
        double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();
        double scaleY = pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();
    node.getTransforms().add(new Scale(scaleX, scaleY));
    PrinterJob job = PrinterJob.createPrinterJob(printer);
    if (job != null) {
        System.out.println("Job created!");
        boolean success = job.printPage(node);
        if (success) {
            System.out.println("Job successfully finished!");
            job.endJob();
        } else {
            System.out.println("Job NOT successful!");
        }
    }
Xdg
  • 1,735
  • 2
  • 27
  • 42

2 Answers2

7

You can use the class PrintHelper which has access to the package private constructor of Paper.

Paper photo = PrintHelper.createPaper("10x15", 100, 150, Units.MM);
mikevinmike
  • 369
  • 3
  • 5
4

The constructor of Paper is package-private so you can't create a Paper size apart from the standard sizes listed in the class.

However you could create a custom size with reflection:

Constructor<Paper> c = Paper.class.getDeclaredConstructor(String.class,
                                         double.class, double.class, Units.class);
c.setAccessible(true);
Paper photo = c.newInstance("10x15", 100, 150, MM);

I have not tested if that works properly though.

assylias
  • 321,522
  • 82
  • 660
  • 783