【问题标题】:Java - set printing page formatJava - 设置打印页面格式
【发布时间】:2014-09-19 14:26:55
【问题描述】:

我尝试使用 DocPrintJob 打印 pdf,但它不适合页面,因此我必须手动打印页面。 我的 pdf 已经是 Letter 格式。

这是我到目前为止所做的:

            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
            PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
            PrintService service = ServiceUI.printDialog(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(), 200, 200,
                                  printService, defaultService, flavor, pras);

            if (service != null) {
                pras.add(MediaSizeName.NA_LETTER);
                PageFormat page = new PageFormat();
/*              MediaSizeName media = MediaSize.findMedia(
                        (float) (page.getWidth() / 72.0),
                        (float) (page.getHeight() / 72.0), 
                        Size2DSyntax.INCH);
    */          
        //      pras.add(media);
                DocPrintJob job = service.createPrintJob();
                FileInputStream fis=null;
                File file = new File("test.pdf");
                try {
                    fis = new FileInputStream("test.pdf");

                } catch (FileNotFoundException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
                DocAttributeSet das = new HashDocAttributeSet();

               Doc document = null;
                document = new SimpleDoc(fis, flavor, null);


                try {
                    job.print(document, pras);
                } catch (PrintException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }


            }

如您所见,我尝试使用 MediaSizeName.NA_LETTER,但它并没有改变任何东西。 有没有办法让用户选择媒体尺寸,打印对话框中没有选项? 提前致谢!

【问题讨论】:

    标签: java pdf printing


    【解决方案1】:

    似乎打印机正在根据您设置的首选项使用默认纸张,因此如果您将首选项设置为使用 letter 尺寸,它会要求使用 letter 尺寸的页面,默认情况下它将在 A4 尺寸的纸张上打印。

    【讨论】:

    • 我尝试在 Letter size: "pras.add(MediaSizeName.NA_LETTER);" 上打印,但它不起作用,所以不应该使用默认 A4 尺寸的纸张吗?
    • 显示此示例代码:programcreek.com/java-api-examples/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    相关资源
    最近更新 更多