java利用jacob將word轉pdf
本文實例為大家分享了java開發(fā)利用jacob將word轉pdf的具體代碼,供大家參考,具體內容如下
jacob 缺點:需要 window 環(huán)境,而且速度是最慢的需要安裝 msofficeWord 以及 SaveAsPDFandXPS.exe ( word 的一個插件,用來把 word 轉化為 pdf )
開發(fā)流程:
SaveAsPDFandXPS 下載地址
jacob 包下載地址:
1、先安裝SaveAsPDFandXPS
2、下載 jacob 解壓后存放路徑:
jacob.jar 放在 C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext目錄下
jacob.dll 放在 C:\Program Files\Java\jdk1.8.0_171\jre\bin 目錄下
實現(xiàn)代碼如下:
package com.casf.hn.core.util;
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* 效果最好的一種方法,但是需要 window 環(huán)境,而且速度是最慢的需要安裝 msofficeWord 以及 SaveAsPDFandXPS.exe (
* word 的一個插件,用來把 word 轉化為 pdf,可以不用安裝,本次未安裝測試通過 )
*
*
*
*/
public class WordToPdf {
private static final int wdFormatPDF = 17; // PDF 格式
public void wordToPDF(String sfileName, String toFileName) {
System.out.println("啟動 Word...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
System.out.println("打開文檔..." + sfileName);
System.out.println("轉換文檔到 PDF..." + toFileName);
File tofile = new File(toFileName);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc, "SaveAs", toFileName, // FileName
wdFormatPDF);
long end = System.currentTimeMillis();
System.out.println("轉換完成..用時:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文檔轉換失?。? + e.getMessage());
} finally {
Dispatch.call(doc, "Close", false);
System.out.println("關閉文檔");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
// 如果沒有這句話,winword.exe進程將不會關閉
ComThread.Release();
}
public static void main(String[] args) {
WordToPdf d = new WordToPdf();
d.wordToPDF("D:\\cssj\\xxxx.doc", "D:\\cssj\\xxxx.pdf");
}
}
運行結果:



以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
mybatis insert foreach循環(huán)插入方式
這篇文章主要介紹了mybatis insert foreach循環(huán)插入方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Mybatis-Plus集成Sharding-JDBC與Flyway實現(xiàn)多租戶分庫分表實戰(zhàn)
這篇文章主要為大家介紹了Mybatis-Plus集成Sharding-JDBC與Flyway實現(xiàn)多租戶分庫分表實戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
Spring Cloud下基于OAUTH2認證授權的實現(xiàn)示例
這篇文章主要介紹了Spring Cloud下基于OAUTH2認證授權的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
SpringBoot安全認證Security的實現(xiàn)方法
這篇文章主要介紹了SpringBoot安全認證Security的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

