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