資源簡介
因使用java語言實現(xiàn)將word轉(zhuǎn)pdf功能,在windows和linux上的實現(xiàn)是不一樣的。本代碼案例分別介紹了在linux和windows上如何使用java語言實現(xiàn)word轉(zhuǎn)pdf,特別實用,解決了在轉(zhuǎn)換過程中元素缺少(下劃線轉(zhuǎn)化成pdf后消失)的問題。

代碼片段和文件信息
package?TestProject.com.edu.ouc;
import?java.io.File;
import?com.jacob.activeX.ActiveXComponent;
import?com.jacob.com.Dispatch;
public?class?Word2Pdf?{
public?static?void?main(String?args[])?{
????????ActiveXComponent?app?=?null;
????????String?wordFile?=?“e:/檢驗報告.docx“;
???????String?pdfFile?=?“e:/個人成長規(guī)劃報告.pdf“;
???????System.out.println(“開始轉(zhuǎn)換...“);
???????//System.out.println(System.getProperty(“java.library.path“));
???????//?開始時間
???????long?start?=?System.currentTimeMillis();??
???????try?{
????????//?打開word
????????app?=?new?ActiveXComponent(“Word.Application“);
????????//?設(shè)置word不可見很多博客下面這里都寫了這一句話,其實是沒有必要的,因為默認就是不可見的,如果設(shè)置可見就是會打開一個word文檔,對于轉(zhuǎn)化為pdf明顯是沒有必要的
????????//app.setProperty(“Visible“?false);
????????//?獲得word中所有打開的文檔
????????Dispatch?documents?=?app.getProperty(“Documents“).toDispatch();
????????System.out.println(“打開文件:?“?+?wordFile);
????????//?打開文檔
????????Dispatch?document?=?Dispatch.call(documents?“Open“?wordFile?false?true).toDispatch();
????????//?如果文件存在的話,不會覆蓋,會直接報錯,所以我們需要判斷文件是否存在
????????File?target?=?new?File(pdfFile);??
?????????if?(target.exists())?{??
????????????target.delete();
?????????}
????????System.out.println(“另存為:?“?+?pdfFile);
????????//?另存為,將文檔報錯為pdf,其中word保存為pdf的格式宏的值是17
????????Dispatch.call(document?“SaveAs“?pdfFile?17);
????????//?關(guān)閉文檔
????????Dispatch.call(document?“Close“?false);
????????//?結(jié)束時間
????????long?end?=?System.currentTimeMillis();
????????System.out.println(“轉(zhuǎn)換成功,用時:“?+?(end?-?start)?+?“ms“);
???????}catch(Exception?e)?{
????????System.out.println(“轉(zhuǎn)換失敗“+e.getMessage());
???????}finally?{
????????????//?關(guān)閉office
????????//app.invoke(“Quit“?0);
???????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????378865??2018-06-09?11:07??27、Word轉(zhuǎn)PDF\jacob-1.14.3.zip
?????文件????8544036??2018-08-03?16:55??27、Word轉(zhuǎn)PDF\Linux平臺\aspose-words-14.9.0-jdk16.jar
?????文件????????586??2018-08-03?16:49??27、Word轉(zhuǎn)PDF\Linux平臺\license.xm
?????文件??????17275??2018-08-03?20:48??27、Word轉(zhuǎn)PDF\Linux平臺\WriteToWord.java
?????文件????????110??2018-08-03?20:46??27、Word轉(zhuǎn)PDF\Linux平臺\說明.txt
?????文件??????38647??2018-08-01?19:02??27、Word轉(zhuǎn)PDF\ReportDemo.docx
?????文件?????378865??2018-06-09?11:07??27、Word轉(zhuǎn)PDF\Window平臺\jacob-1.14.3.zip
?????文件???????2089??2018-08-03?16:03??27、Word轉(zhuǎn)PDF\Window平臺\Word2Pdf.java
?????文件?????????62??2018-08-03?20:41??27、Word轉(zhuǎn)PDF\Window平臺\轉(zhuǎn)pdf說明.txt
?????文件???????2086??2018-06-09?11:05??27、Word轉(zhuǎn)PDF\Word2Pdf.java
?????文件????????364??2018-06-09?11:12??27、Word轉(zhuǎn)PDF\備注.txt
?????目錄??????????0??2018-08-27?08:58??27、Word轉(zhuǎn)PDF\Linux平臺
?????目錄??????????0??2018-08-27?08:58??27、Word轉(zhuǎn)PDF\Window平臺
?????目錄??????????0??2018-10-09?10:08??27、Word轉(zhuǎn)PDF
-----------?---------??----------?-----??----
??????????????9362985????????????????????14
評論
共有 條評論