能安裝第三方軟體的話,可以考慮以使用Spire.Doc for Java:
你可以在Java程序中添加 Spire.Doc.jar 文件作為依賴項。可以從這個鏈接下載 JAR 文件;如果使用Maven,則可以通過在 pom.xml 文件中添加以下代碼導入 JAR 文件。
repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository></repositories><dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.3</version>
</dependency></dependencies>
將Word轉換成PDF:
Spire.Doc for Java擁有強大的文件轉換功能,其提供了 Document. saveToFile(String fileName, FileFormat fileFormat) 方法可將 Word 文檔轉為多種格式的目標文件,下面是轉為 PDF 的方法步驟:
實例化Document類的對象。
調用Document.loadFromFile()方法載入 Word 文檔。
調用Document.saveToFile()方法將 Word 保存為 PDF 格式,並指定保存路徑。
Java代碼如下:
import com.spire.doc.*;public class WordToPDF{
public static void main(String[] args) {
//實例化Document類的對象
Document doc = new Document();
//載入Word
doc.loadFromFile("測試.docx");
//保存為PDF格式
doc.saveToFile("WordToPDF.pdf",FileFormat.PDF);
}
}
希望對您有幫助。
2. 使用java 將word 轉為 PDF時, 取消 自動識別標簽並轉化為「帶有標簽的PDF」
創建Pdf時,其內容未加標簽。僅當Pdf是加標簽的Pdf時,方可將注釋導出到Word文檔。
在「導航欄」右鍵選擇「添加/編輯工具集」,中選擇添加「輔助工具」裡面的「添加標簽到文檔」
就會在「導航欄」中出現下面用紅色圈的「按鈕」:
點擊這個以後,Acrobat就會自動識別標簽並轉化為「帶有標簽的PDF」,就不會出現你「顯示的這個錯誤了」。
3. java word轉pdf
可以試試spire.doc for java轉換Word到PDF
4. 在linux環境下,java怎麼實現從word格式轉換為pdf格式
WORD轉換到PDF
WORD轉換到PDF的步驟就相對簡單了,我們只需要安裝一款虛擬列印機軟體「Virtual Pdf Printer」即可,它可以將你編輯好的WORD文檔直接輸出為PDF文件格式。(下載地址: http://www.newhua.com/soft/21190.htm)
安裝後在WORD中點擊「打開」→「列印」,在「列印機名稱」中會看到多了一個「Virtual Printer」選項,選中它,點擊確定後(未注冊版本有10秒的等待時間),彈出「保存PDF文件」對話框,先選擇文件的保存路徑,再點擊「生成設置」按鈕,彈出「系統設置」面板(如圖3),對輸出後的PDF文件進行加密和字體等設置,點擊兩次確定,當前的WORD文檔就被轉換成PDF格式的文件了
5. 在linux環境下,java怎麼實現從word格式轉換為pdf格式
Word轉換成pdf格式是比較簡單的,我們使用pdf轉換器就可以輕松將word轉換成pdf格式
參考軟體:迅捷pdf轉換器
參考步驟:第一步:打開桌面上的迅捷pdf轉換器,然後選擇「其他文件轉換成PDF」,再選擇「word轉成pdf」
第三步:轉換好之後選擇「輸出」,把轉換好的文件輸出保存下來就可以了
6. 在linux環境下,java怎麼實現從word格式轉換為pdf格式
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* @author XuMing Li
*
* @version 1.00, 2007-4-9
*
*/
public class D2P {
private ActiveXComponent wordCom = null;
private Object wordDoc = null;
private final Variant False = new Variant(false);
private final Variant True = new Variant(true);
/**
* 打開word文檔
*
* @param filePath
* word文檔
* @return 返回word文檔對象
*/
public boolean openWord(String filePath) {
//建立ActiveX部件
wordCom = new ActiveXComponent( "Word.Application ");
try {
//返回wrdCom.Documents的Dispatch
Dispatch wrdDocs = wordCom.getProperty( "Documents ").toDispatch();
//調用wrdCom.Documents.Open方法打開指定的word文檔,返回wordDoc
wordDoc = Dispatch.invoke(wrdDocs, "Open ", Dispatch.Method,
new Object[] { filePath }, new int[1]).toDispatch();
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
/**
* 關閉word文檔
*/
public void closeWord() {
//關閉word文件
wordCom.invoke( "Quit ", new Variant[] {});
}
/**
* * 將word文檔列印為PS文件後,使用Distiller將PS文件轉換為PDF文件 *
*
* @param sourceFilePath
* 源文件路徑 *
* @param destinPSFilePath
* 首先生成的PS文件路徑 *
* @param destinPDFFilePath
* 生成PDF文件路徑
*/
public void docToPDF(String sourceFilePath, String destinPSFilePath,
String destinPDFFilePath) {
if (!openWord(sourceFilePath)) {
closeWord();
return;
}
//建立Adobe Distiller的com對象
ActiveXComponent distiller = new ActiveXComponent(
"PDFDistiller.PDFDistiller.1 ");
try {
//設置當前使用的列印機,我的Adobe Distiller列印機名字為 "Adobe PDF "
wordCom.setProperty( "ActivePrinter ", new Variant( "Adobe PDF "));
//設置printout的參數,將word文檔列印為postscript文檔。目前只使用了前5個參數,如果要使用更多的話可以參考MSDN的office開發相關api
//是否在後台運行
Variant Background = False;
//是否追加列印
Variant Append = False;
//列印所有文檔
int wdPrintAllDocument = 0;
Variant Range = new Variant(wdPrintAllDocument);
//輸出的postscript文件的路徑
Variant OutputFileName = new Variant(destinPSFilePath);
Dispatch.callN((Dispatch) wordDoc, "PrintOut ", new Variant[] {
Background, Append, Range, OutputFileName });
System.out.println( "由word文檔列印為ps文檔成功! ");
//調用Distiller對象的FileToPDF方法所用的參數,詳細內容參考Distiller Api手冊
//作為輸入的ps文檔路徑
Variant inputPostScriptFilePath = new Variant(destinPSFilePath);
//作為輸出的pdf文檔的路徑
Variant outputPDFFilePath = new Variant(destinPDFFilePath);
//定義FileToPDF方法要使用adobe pdf設置文件的路徑,在這里沒有賦值表示並不使用pdf配置文件
Variant PDFOption = new Variant( " ");
//調用FileToPDF方法將ps文檔轉換為pdf文檔
Dispatch.callN(distiller, "FileToPDF ", new Variant[] {
inputPostScriptFilePath, outputPDFFilePath, PDFOption });
System.out.println( "由ps文檔轉換為pdf文檔成功! ");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
closeWord();
}
}
public static void main(String[] argv) {
D2P d2p = new D2P();
// d2p.openWord( "c:/12.doc ");
// d2p.callWordMacro( "c:/12.docc ", "MyWordMacro ",
// new String[] { "這是調用word宏的測試程序 " });
d2p.docToPDF( "d:/12.doc ", "c:/1p.ps ", "c:/1p.pdf ");
}
}
7. Java如何將Word文件轉成PDF文件
用Java內嵌iText生成PDF文檔需要5個步驟:
①建立com.lowagie.text.Document對象的實例。
Document document = new Document();
②建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中。
PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));
③打開文檔。
document.open();
④向文檔中添加內容。
document.add(new Paragraph("Hello World"));
⑤關閉文檔。
document.close();
通過上面的5個步驟,就能產生一個Helloworld.PDF的文件,文件內容為"Hello World"。