Java calls printer printing file, java calls printer printing

Source: Internet
Author: User
Tags print format

Java calls printer printing file, java calls printer printing

/*** Print ** @ param file * @ param saveFile * @ param preFix * @ param formatFile * @ return * @ throws NoOfficeException */public static void doPrintDoc (File file) {if (file = null |! File. exists () {return;} // get the file suffix 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 "; property =" Documents ";} else if (extName. equals ("XLS") | extName. equals ("X LSX ") | 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 ComThread component. 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 (); Dispatch doc = null; // if it is a 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. getAbsol UtePath ()}, new int [1]). toDispatch ();} Dispatch. call (doc, "PrintOut"); Dispatch. call (doc, "Close"); if (! Property. equals ("Presentations") {axc. invoke ("Quit", new Variant [] {}) ;}} catch (Exception e) {e. printStackTrace () ;}finally {comApp = ""; property = ""; ComThread. release ();}}
 
Required jar and dll download path http://download.csdn.net/detail/daixinmei/7962251
Dll files must be stored in windows/system32, or in the bin directory of the jdk installation directory.


How can I use java to call the default printer to print documents?

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet ();
// Set the print format. Because the file type is not determined, select AUTOSENSE.
DocFlavor flavor = DocFlavor. INPUT_STREAM.AUTOSENSE;
// Find all available printing services
PrintService printService [] = PrintServiceLookup. lookupPrintServices (flavor, pras );
// Locate the default Print Service
PrintService defaultService = PrintServiceLookup. lookupdefaprintprintservice ();
// Display the Print dialog box
PrintService service = ServiceUI. printDialog (null, 200,200, printService, defaultService, flavor, pras );
If (service! = Null ){
Try {
DocPrintJob job = service. createPrintJob (); // create a print job
FileInputStream FCM = new FileInputStream (file); // construct the file stream to be printed
DocAttributeSet das = new HashDocAttributeSet ();
Doc doc = new SimpleDoc (FCM, flavor, das); // create a print file format
Job. print (doc, pras); // print the file
} Catch (Exception e ){
E. printStackTrace ();
}
}
 
Printer printing is called by java programs.

The second one ..

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.