Java calls printer to print files

Source: Internet
Author: User

/** * for printing * * @param file * @param saveFile * @param preFix * @param formatfile * @return * @throws noofficeexception * * public static void Doprintdoc (file file) {if (file = = NULL | |!file.exists ()) {return;} Gets the file suffix name int index = File.getname (). IndexOf ("."); String extname = File.getname (). toUpperCase (). substring (index + 1); String Comapp = "Word.Application"; String property = "Documents";//if it is word if (extname.equals ("DOC") | | | extname.equals ("DOCX") | | extname.equals ("WPS")) {Comapp = "Word.Application";p roperty = "Documents";} else if (extname.equals ("XLS") | | | extname.equals ("XLSX") | | extname.equals ("ET")) {Comapp = "Excel.Application"; property = "Workbooks";} else if (extname.equals ("PPT") | | | extname.equals ("PPTX") | | extname.equals ("DPS")) {Comapp = "powerpoint.application"; property = "Presentations";} Initialize the component Comthread.initsta ();//define the Component Object Activexcomponent AXC = new Activexcomponent (Comapp); try {if (!property.equals (" Presentations ")) {Dispatch.put (AXC," Visible ", new Variant (false));} Dispatch document = Axc.getproperty (property). Todispatch ();D Ispatch doc = null;//if it is ppt, if (Property.equals (" Presentations ")) {doc = Dispatch.call (document," Open ", File.getabsolutepath (), True, True, false). Todispatch ();} else { doc = Dispatch.invoke (document, "Open", Dispatch.method, new object[] {File.getabsolutepath ()}, new Int[1]). Todispatch ( );} Dispatch.call (Doc, "PrintOut");D Ispatch.call (Doc, "Close"), if (!property.equals ("Presentations")) {Axc.invoke (" Quit ", new variant[] {});}} catch (Exception e) {e.printstacktrace ();} finally {Comapp = "";p roperty = ""; Comthread.release ();}}
Required jar and DLL download path http://download.csdn.net/detail/daixinmei/7962251
DLL to be placed under Windows/system32, or under the bin of the JDK installation directory

Java calls printer to print files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.