JavaFX print custom paper size


Question

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!");
        }
    }
1
2
11/5/2013 6:27:59 PM

Accepted Answer

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.

2
3/25/2014 9:42:49 AM

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon