It says how Java generates complex Word documents this year, documenting how Java calls printers to print Word documents.
The original use of the PrintJob, but the system provides a printing mechanism than immature complete. The code on the Web is also a cookie-cutter, on my print device Canon ir2525/2530 Ufrii LT, I can get to the printer's various properties, the current number of tasks. But the printer didn't respond.
Refer to these two articles:
Http://www.360doc.com/content/05/0916/11/332_12789.shtml
http://www.ibm.com/developerworks/cn/java/l-javaprint/
Toss for a long time, the final decision by using a third party, found Jacob, also good, hereby recorded.
1. Copy Jacob's DLL file to C:\Windows\System32.
2. Open service DCOM Server Process Launcher (dcomlaunch) in Task Manager.
3. Copy the Jacob.jar to the project's/libs directory.
Note that I cannot find the DLL file on Windows Server 2008, Error: Exception in thread "main" Java.lang.UnsatisfiedLinkError:no jacob-1.17-x86 In Java.library.path
But on the win system, it's okay.
The code is as follows:
Public classTestdoc { Public Static voidMain (string[] args) {String path= "D:\\yanqiong.doc"; System.out.println ("Start Printing"); Comthread.initsta (); Activexcomponent Word=NewActivexcomponent ("Word.Application"); Dispatch Doc=NULL; Dispatch.put (Word,"Visible",NewVariant (false)); Dispatch Docs=word.getproperty ("Documents"). Todispatch (); Doc=dispatch.call (Docs, "Open", Path). Todispatch (); Try{Dispatch.call (doc,"PrintOut");//Print}Catch(Exception e) {e.printstacktrace (); System.out.println ("Print Failed"); }finally{ Try { if(doc!=NULL) {Dispatch.call (doc,"Close",NewVariant (0)); } } Catch(Exception E2) {e2.printstacktrace (); } //Freeing Resourcescomthread.release (); } }}
Example code:
Http://pan.baidu.com/s/1o7NrPcY up56
Java Jacob calls printer to print Word document