jacob將WORD轉換成PDF文件(要裝有Word另存爲PDF文件的插件)

public class Dispatch_MSWord {
	static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
	static final int wdFormatPDF = 17;// PDF 格式

	
	public void ofsevenToPDF(String sfileName,String toFileName){
		
		//String filename = "D:\\補充保密協議.doc";  
	    //String toFilename = sfileName + ".pdf";  
	    
	    System.out.println("啓動Word...");  
	    long start = System.currentTimeMillis();  
	    ActiveXComponent app = null;  
	    try {  
	        app = new ActiveXComponent("Word.Application");  
	        app.setProperty("Visible", false);  
	  
	        Dispatch docs = app.getProperty("Documents").toDispatch();  
	        System.out.println("打開文檔..." + sfileName);  
	        Dispatch doc = Dispatch.call(docs,//  
						                 "Open", //  
						                 sfileName,// FileName  
						                 false,// ConfirmConversions  
						                 true // ReadOnly  
						                 ).toDispatch();  
	  
	        System.out.println("轉換文檔到PDF..." + toFileName);  
	        File tofile = new File(toFileName);  
	        if (tofile.exists()) {  
	            tofile.delete();  
	        }  
	        Dispatch.call(doc,//  
		                  "SaveAs", //  
		                  toFileName, // FileName  
		                  wdFormatPDF);  
	  
	        Dispatch.call(doc, "Close", false);  
	        
	        long end = System.currentTimeMillis();  
	        System.out.println("轉換完成..用時:" + (end - start) + "ms.");  
	        
	    } catch (Exception e) {  
	        System.out.println("========Error:文檔轉換失敗:" + e.getMessage());  
	    } finally {  
	        if (app != null)  
	            app.invoke("Quit", wdDoNotSaveChanges);  
	    }  
		
	}


  運行此代碼,必要要機器上的WORD中要裝有另存爲PDF的插件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章