JavaFX打印自定义纸张大小

时间:2013-11-05 18:28:00

标签: javafx

在JavaFX中,我想将照片打印成10x15纸张。有一些Paper constansts,但没有100x150 mm的常数。

是否可以创建自己的Paper以在PageLayout中使用它?

感谢。

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!");
        }
    }

2 个答案:

答案 0 :(得分:5)

您可以使用类PrintHelper,它可以访问Paper的包私有构造函数。

Paper photo = PrintHelper.createPaper("10x15", 100, 150, Units.MM);

答案 1 :(得分:2)

Paper的构造函数是包私有的,因此除了类中列出的标准尺寸之外,您无法创建纸张大小。

但是,您可以使用反射创建自定义尺寸:

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

我还没有测试过它是否正常工作。

相关问题