简体   繁体   English

无法使用Java打印机服务(JPS)打印文档

[英]Unable to print the document using Java printer Services(JPS)

I have implemented a program, to print the document to the specific printer using IP address, printer name and running fine with out any errors and exception. 我已经实现了一个程序,可以使用IP地址,打印机名称将文档打印到特定的打印机,并且运行正常,没有任何错误和异常。 A printer job is being sent from java, I am able to see this on my local printer print pool, but the page is not printing on printer. 正在从Java发送打印机作业,我可以在本地打印机打印池上看到此作业,但是该页面无法在打印机上打印。

URI myURI=null;
FileInputStream psStream=null;
try   {
    psStream = new FileInputStream("sample.doc");
}
catch ( FileNotFoundException e )   {
    e.printStackTrace();
}
DocFlavor psInFormat = DocFlavor.BYTE_ARRAY.GIF;
Doc myDoc = new SimpleDoc( psStream, psInFormat, null );
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
PrintService[] services = PrintServiceLookup.lookupPrintServices( psInFormat, aset);
if ( services.length > 0 ) {
    DocPrintJob job = services[0].createPrintJob();         
    try {
        job.print( myDoc, aset);
    }
    catch ( PrintException e ){
    }
}

Could you please help me out on this? 你能帮我这个忙吗?

Thanks, Srikanth Chilukuri 谢谢,Srikanth Chilukuri


I found the problem. 我发现了问题。

psStream = new FileInputStream("sample.doc");    

The above statement creating problem. 上面的陈述造成了问题。 Because It is MS Word Application, So unable to read the file using File Input Stream. 因为它是MS Word应用程序,所以无法使用文件输入流读取文件。

I am using POI jar and reading the doc. 我正在使用POI jar并阅读文档。

POIFSFileSystem psStream = new POIFSFileSystem(new FileInputStream(filesname));
Doc myDoc = new SimpleDoc( psStream, psInFormat, null );

But Doc API is not supporing got IllegalArgumentException 但是Doc API不支持IllegalArgumentException

Exception in thread "Main Thread" java.lang.IllegalArgumentException: data is not of declared type
    at javax.print.SimpleDoc.<init>(SimpleDoc.java:82)
    at com.src.print.TestPrint2.main(TestPrint2.java:67)

Could you please help me out on this. 你能帮我这个忙吗?

Thanks for your response 感谢您的答复

I guess you are talking about the AWT print. 我猜您在谈论AWT打印。 This is different from Java Print Service. 这与Java Print Service不同。 You can have have preformatted text data printed using a variety of options using Java Print Service(JPS) 您可以使用Java Print Service(JPS)使用多种选项来打印预格式化的文本数据。

http://docs.oracle.com/javase/1.5.0/docs/guide/jps/spec/docflavor.fm1.html#998469 http://docs.oracle.com/javase/1.5.0/docs/guide/jps/spec/docflavor.fm1.html#998469

The problem comes in when the file is encoded using UTF-8 and you try to print this using JPS if its a normal ASCII file. 当使用UTF-8编码文件时,如果它是普通ASCII文件,则尝试使用JPS打印此文件时,就会出现问题。 it gets printed correctly 它被正确打印

Java Print Services allow you to "draw" pages like Swing and then send the result to a printer. Java Print Services允许您“绘制”页面(例如Swing),然后将结果发送到打印机。 It does not have knowledge of Word formats or HTML pages or similar. 它不具备Word格式或HTML页面或类似内容的知识。

Therefore you need a module which knows how to draw the contents of a doc-file to a printer to do this. 因此,您需要一个知道如何将文档文件的内容绘制到打印机上的模块。 I do not personally have experience with such a module. 我个人没有此类模块的经验。

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

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