开发者

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. 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.

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


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.

I am using POI jar and reading the doc.

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

But Doc API is not supporing got 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.开发者_Python百科src.print.TestPrint2.main(TestPrint2.java:67)

Could you please help me out on this.


Java Print Services allow you to "draw" pages like Swing and then send the result to a printer. It does not have knowledge of Word formats or HTML pages or similar.

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.


Thanks for your response

I guess you are talking about the AWT print. This is different from Java Print Service. You can have have preformatted text data printed using a variety of options using Java Print Service(JPS)

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. it gets printed correctly

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜