小编sah*_*rsh的帖子

使用java在网络打印机中打印pdf

伙计我在使用java打印pdf时遇到困难.我写的代码如下:

`

public static void main(String[] args) throws PrinterException, PrintException, IOException{
        DocFlavor docflavor = new DocFlavor.INPUT_STREAM ("application/octet-stream");
    //  DocFlavor docflavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
/*      DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.;*/
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        DocFlavor[] docF = printService.getSupportedDocFlavors();
        for(int i = 0; i<docF.length;i++){
            System.out.println(docF[i]);
        }
        FileInputStream fis = new FileInputStream("pathofpdffile");



        Doc pdfDoc = new SimpleDoc(fis, docflavor, null);


        DocPrintJob printJob = printService.createPrintJob();
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

        aset.add(new Copies(1));
        aset.add(Sides.ONE_SIDED);
        printJob.print(pdfDoc,aset);

        fis.close();
Run Code Online (Sandbox Code Playgroud)

}`

上面的代码启动了打印活动,但问题是我只能在打印中编码字符.我无法得到我的确切文件.

其次,如果我将DocFlavor更改为SERVICE_FORMATTED.PAGEABLE,则会引发错误

java.lang.IllegalArgumentException: data is not of declared type
at javax.print.SimpleDoc.<init>(Unknown …
Run Code Online (Sandbox Code Playgroud)

java printing pdf

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

pdf ×1

printing ×1