java利用jacob將word轉(zhuǎn)pdf
本文實例為大家分享了java開發(fā)利用jacob將word轉(zhuǎn)pdf的具體代碼,供大家參考,具體內(nèi)容如下
jacob 缺點:需要 window 環(huán)境,而且速度是最慢的需要安裝 msofficeWord 以及 SaveAsPDFandXPS.exe ( word 的一個插件,用來把 word 轉(zhuǎn)化為 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 轉(zhuǎn)化為 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("轉(zhuǎn)換文檔到 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("轉(zhuǎn)換完成..用時:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文檔轉(zhuǎn)換失敗:" + e.getMessage());
} finally {
Dispatch.call(doc, "Close", false);
System.out.println("關(guān)閉文檔");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
// 如果沒有這句話,winword.exe進程將不會關(guān)閉
ComThread.Release();
}
public static void main(String[] args) {
WordToPdf d = new WordToPdf();
d.wordToPDF("D:\\cssj\\xxxx.doc", "D:\\cssj\\xxxx.pdf");
}
}
運行結(jié)果:



以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+Vue項目部署實現(xiàn)傳統(tǒng)方式
我們在進行前后端分離開發(fā)的時候,一般是將前端項目部署到nginx服務(wù)器上,與后端項目分開部署,這篇文章主要給大家介紹了關(guān)于SpringBoot+Vue項目部署實現(xiàn)傳統(tǒng)方式的相關(guān)資料,需要的朋友可以參考下2024-01-01
Java 網(wǎng)絡(luò)編程socket編程等詳解
本篇文章主要介紹了java網(wǎng)絡(luò)編程中的類的方法以及實例,需要的朋友可以參考下2017-04-04
基于Java字符串 "==" 與 "equals" 的深入理解
本篇文章是對Java中的字符串"=="與"equals"進行了詳細的分析介紹,需要的朋友參考下2013-06-06

