问题描述
伙计们,我一直在使用 java 打印 pdf.我写的代码如下:
Guys I am stuck in printing a pdf using java.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.
第二,如果我将 DocFlavor 更改为 SERVICE_FORMATTED.PAGEABLE ,它会引发错误
Second if I change the DocFlavor to SERVICE_FORMATTED.PAGEABLE ,it throws an error
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)
第三,如果我将 DocFlavor 更改为 INPUT_STREAM.PDF,它会抛出错误
Third IF I change the DocFlavor to INPUT_STREAM.PDF, it throws as error
`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)`
所有这些我都在网络打印机上尝试.任何帮助都会gr8..
All this I am trying on a network printer.Any help would gr8..
推荐答案
只需更改您的代码以使用 AUTO_SENSE,如下所示.
Just change your code to use AUTO_SENSE as shown below.
InputStream inputStream = new FileInputStream("C://test.pdf");
Doc doc = new SimpleDoc(inputStream, DocFlavor.INPUT_STREAM.AUTOSENSE,null);
这篇关于使用java在网络打印机中打印pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!