修改介质名称 Java Apache FOP API 的打印属性

发布于 2024-11-24 03:23:22 字数 1568 浏览 2 评论 0原文

我正在使用 Apache FOP API 打印一份文档,该文档在一段时间内运行良好,但现在它正在尝试在纸盒 1 上的合法尺寸纸张上打印。我想知道我是否可以将其更改为 Letter 尺寸,以便用户不必手动打印点击打印机上的按钮即可实现这一点。

public void printDocument() {
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintRequestAttributeSet aset =
            new HashPrintRequestAttributeSet();
    PrintService prnSvc = null;

    /* locate a print service that can handle it */
    PrintService[] pservices =
            PrintServiceLookup.lookupPrintServices(null, null);
    if (pservices.length > 0) {
        int ii = 0;
        while (ii < pservices.length) {
            System.out.println("Named Printer found: " + pservices[ii].getName());
            if (pservices[ii].getName().endsWith("xyz")) {
                prnSvc = pservices[ii];
                System.out.println("Named Printer selected: " + pservices[ii].getName() + "*");
                break;
            }
            ii++;
        }

        /* create a print job for the chosen service */
        DocPrintJob pj = prnSvc.createPrintJob();
        try {
            File file = new File("test.pcl");
            FileInputStream fis = new FileInputStream(file); //Doc encapsulating the print data
            Doc doc = new SimpleDoc(fis, flavor, null);
            /* print the doc as specified */
            pj.print(doc, aset);
        } catch (IOException ie) {
            System.err.println(ie);
        } catch (PrintException e) {
            e.printStackTrace();
            System.err.println(e);
        }
    }
}

如果有人可以提供任何类似的建议,我将不胜感激。

Am using Apache FOP API to print a document which was working well for a while but now it is trying to print on a legal size paper on tray 1. Am wondering if i can change that to Letter size so that users do not manually have to hit button on the printer to make that happen.

public void printDocument() {
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintRequestAttributeSet aset =
            new HashPrintRequestAttributeSet();
    PrintService prnSvc = null;

    /* locate a print service that can handle it */
    PrintService[] pservices =
            PrintServiceLookup.lookupPrintServices(null, null);
    if (pservices.length > 0) {
        int ii = 0;
        while (ii < pservices.length) {
            System.out.println("Named Printer found: " + pservices[ii].getName());
            if (pservices[ii].getName().endsWith("xyz")) {
                prnSvc = pservices[ii];
                System.out.println("Named Printer selected: " + pservices[ii].getName() + "*");
                break;
            }
            ii++;
        }

        /* create a print job for the chosen service */
        DocPrintJob pj = prnSvc.createPrintJob();
        try {
            File file = new File("test.pcl");
            FileInputStream fis = new FileInputStream(file); //Doc encapsulating the print data
            Doc doc = new SimpleDoc(fis, flavor, null);
            /* print the doc as specified */
            pj.print(doc, aset);
        } catch (IOException ie) {
            System.err.println(ie);
        } catch (PrintException e) {
            e.printStackTrace();
            System.err.println(e);
        }
    }
}

Would highly appreciate if anyone can provide any recommendations around the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2024-12-01 03:23:22

您需要将纸张尺寸添加到 aset 中来指定纸张尺寸:

aset.add(javax.print.attribute.standard.MediaSizeName.<desired paper size>);

MediaSizeName)。对于字母大小,请使用

aset.add(javax.print.attribute.standard.MediaSizeName.NA_LETTER);

You'll need to specify the paper size by adding it to aset:

aset.add(javax.print.attribute.standard.MediaSizeName.<desired paper size>);

(Javadoc for MediaSizeName). For letter size, use

aset.add(javax.print.attribute.standard.MediaSizeName.NA_LETTER);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文