开发者

Modify Printing attribute for Media Name Java Apache FOP API

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++;开发者_JAVA百科
        }

        /* 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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜