2012-02-27 87 views
3

大家好,這是我在網站上的第一個問題。java pdf print NetBeans

我使用:NetBeans IDE 7.0.1,我想用java打印PDF,但我不知道代碼中的錯誤在哪裏,請幫助我。

有一個很好的代碼,其中一個打印一個GIF使用第5個打印機。


PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
pras.add(new Copies(1)); 
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras); 


if (pss.length == 0) throw new RuntimeException("No printer services available."); 
    PrintService ps = pss[5]; 
System.out.println("Printing to " + ps); 
DocPrintJob job = ps.createPrintJob(); 
FileInputStream fin = new FileInputStream("a.gif"); 
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null); 

job.print(doc, pras); 
fin.close(); 

但我不知道什麼是錯以下代碼嘗試打印在同一打印機PDF(5日)


PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
pras.add(new Copies(1)); 
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PDF, pras); 


if (pss.length == 0) throw new RuntimeException("No printer services available."); 
    PrintService ps = pss[5]; 
System.out.println("Printing to " + ps); 
DocPrintJob job = ps.createPrintJob(); 
FileInputStream fin = new FileInputStream("test.PDF"); 
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.PDF, null); 

job.print(doc, pras); 
fin.close(); 

這是程序的答案是:「線程中的異常」main「java.lang.RuntimeException:沒有打印機服務a vailable。 「

如果您有任何想法,請把它寫 感謝

+0

看來關於如何做到這一個博客文章在說您的系統中沒有默認打印機以PDF格式打印。您是否看過這個:http://stackoverflow.com/questions/7355025/create-pdf-with-java – assylias 2012-02-27 15:04:19

+0

不幸的是,我有一個默認打印機,所以問題不是這樣 – 2012-02-27 15:27:17

+0

您的默認打印機是否支持PDF流作爲直接輸入?因爲在我看來,這是您正在嘗試做的...... – yms 2012-02-27 15:37:14

回答