java 調用印表機列印檔案,java調用印表機列印

來源:互聯網
上載者:User

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程式調用印表機列印的問題

第二個。。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.