JavaFX-以编程方式设置目标路径,以将节点直接打印为pdf文件

时间:2018-10-07 00:18:35

标签: pdf javafx printing

我想使用“ Microsoft Print to PDF”打印机将节点打印到pdf文件。假设已经提取了Printer对象,那么我的下一个功能可以正常运行。

public class Player {
    public static int turn(int x, int y){
        return 3;
    }

    public static void main(String[] args){
        GameGui.main(args); //Class inside JAR
    }
}

我在这里唯一的问题是弹出一个对话框,并询问保存pdf的目标路径。我一直在努力寻找一种以编程方式设置路径的解决方案,但没有成功。有什么建议么?预先谢谢你。

1 个答案:

答案 0 :(得分:1)

经过更多研究后,我遇到了一个丑陋的骇客。我从PrinterJob访问jobImpl私有字段,并从中获取属性。因此,我插入了destination属性,显然它正在按要求工作。我知道这不是很好,但是...是可行的。如果您有更好的建议,请随时发布。

         try {
            java.lang.reflect.Field field = job.getClass().getDeclaredField("jobImpl");
            field.setAccessible(true);
            PrinterJobImpl jobImpl = (PrinterJobImpl) field.get(job);
            field.setAccessible(false);

            field = jobImpl.getClass().getDeclaredField("printReqAttrSet");
            field.setAccessible(true);
            PrintRequestAttributeSet printReqAttrSet = (PrintRequestAttributeSet) field.get(jobImpl);
            field.setAccessible(false);

            printReqAttrSet.add(new Destination(new java.net.URI("file:/C:/deleteMe/wtv.pdf")));
        } catch (Exception e) {
            System.err.println(e);
        }