java 調用印表機列印檔案,java調用印表機列印
/** * 進行列印 * * @param file * @param saveFile * @param preFix * @param formatFile * @return * @throws NoOfficeException */public static void doPrintDoc(File file) {if (file == null || !file.exists()) {return;}// 擷取檔案尾碼名int index = file.getName().indexOf(".");String extName = file.getName().toUpperCase().substring(index + 1);String comApp = "Word.Application";String property = "Documents";// 如果是Word的話if (extName.equals("DOC") || extName.equals("DOCX") || extName.equals("WPS")) {comApp = "Word.Application";property = "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";}// 初始化組件ComThread.InitSTA();// 定義組件對象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;// 如果是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");Dispatch.call(doc, "Close");if (!property.equals("Presentations")) {axc.invoke("Quit", new Variant[] {});}} catch (Exception e) {e.printStackTrace();} finally {comApp = "";property = "";ComThread.Release();}}
需要的jar和dll下載路徑http://download.csdn.net/detail/daixinmei/7962251
dll要放在windows/system32下,或者jdk安裝目錄的bin下
怎使用java調用預設印表機列印文檔?
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//設定列印格式,因為未確定檔案類型,這裡選擇AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//尋找所有的可用列印服務
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位預設的列印服務
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//顯示列印對話方塊
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null) {
try {
DocPrintJob job = service.createPrintJob(); //建立列印工作
FileInputStream fis = new FileInputStream(file); //構造待列印的檔案流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das); //建立列印檔案格式
job.print(doc, pras); //進行檔案的列印
}catch(Exception e) {
e.printStackTrace();
}
}
對於java程式調用印表機列印的問題
第二個。。