JavaFX打印自定义纸张大小 [英] JavaFX print custom paper size

查看:762
本文介绍了JavaFX打印自定义纸张大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Is it possible to create an own Paper to use it in 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!");
        }
    }


推荐答案

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

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.

这篇关于JavaFX打印自定义纸张大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆