简体   繁体   English

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

[英]Print pdf in network printer using java

Guys I am stuck in printing a pdf using java. 伙计我在使用java打印pdf时遇到困难。 The code that I have written is below: 我写的代码如下:

` `

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

}` }`

The above code intiate the printing activity but the problem is that I get only encode character in print. 上面的代码启动了打印活动,但问题是我只能在打印中编码字符。 I am not able to get my exact file. 我无法得到我的确切文件。

Second if I change the DocFlavor to SERVICE_FORMATTED.PAGEABLE ,it throws an error 其次,如果我将DocFlavor更改为SERVICE_FORMATTED.PAGEABLE,则会引发错误

java.lang.IllegalArgumentException: data is not of declared type
at javax.print.SimpleDoc.<init>(Unknown Source)
at com.calculator.main.PrintingTest.main(PrintingTest.java:42)

Third IF I change the DocFlavor to INPUT_STREAM.PDF, it throws as error 第三,如果我将DocFlavor更改为INPUT_STREAM.PDF,它会抛出错误

`Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Unknown Source)
at com.calculator.main.PrintingTest.main(PrintingTest.java:49)`

All this I am trying on a network printer. 所有这些我正在尝试网络打印机。 Any help would gr8.. 任何帮助都会gr8 ..

Just change your code to use AUTO_SENSE as shown below. 只需更改代码即可使用AUTO_SENSE,如下所示。

InputStream inputStream = new FileInputStream("C://test.pdf");
Doc doc = new SimpleDoc(inputStream, DocFlavor.INPUT_STREAM.AUTOSENSE,null);

My first guess would be that new DocFlavor.INPUT_STREAM ("application/octet-stream") isn't what you want. 我的第一个猜测是new DocFlavor.INPUT_STREAM ("application/octet-stream")不是你想要的。

You may want to try the code from this answer: https://stackoverflow.com/a/18962278/34088 您可能想尝试以下答案中的代码: https//stackoverflow.com/a/18962278/34088

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM