簡介
Aspose.Words for Java is a class library that enables your applications to perform a great range of document processing tasks. Aspose.Words supports DOC, DOCX, RTF, HTML, OpenDocument, PDF, XPS, EPUB and other formats. With Aspose.Words you can generate, modify, convert, render and print documents without using Microsoft Word®.
官網文檔:
https://docs.aspose.com/display/wordsjava/Home官網代碼示例:
https://github.com/aspose-words/Aspose.Words-for-Java.git同類别軟體: Apache POI
注:
- Aspose需要商業授權,POI開源免費
- 對于簡單的文檔關鍵字替換使用POI
- 複雜的表格編輯,使用Aspose中的标簽替換
- 對于替換參數(數字,文本,表格,圖檔等),可封裝替換參數,增強代碼的适用性
代碼示例
Java類庫
Maven
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>19.5</version>
<classifier>jdk17</classifier>
</dependency>
直接引入Jar
aspose-words-19.5-jdk17.jar
簡單示例
加載license
try {
License license = new License();
license.setLicense("Aspose.Words.lic");
System.out.println("License set successfully.");
} catch (Exception e) {
System.out.println("There was an error setting the license: " + e.getMessage());
}
Word->PDF
File file = new File("/Test-01.docx");
Document document = new Document(new FileInputStream(file));
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(5);
OutputStream outputStream = new FileOutputStream("/Test-01.pdf");
document.save(outputStream,pdfSaveOptions);
書簽文本替換
document.getRange().getBookmarks().get(bookMakeName).setText(word);