Aspose-Words文档处理类库

mtain 2019年05月29日 433次浏览

简介

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

注:

  1. Aspose需要商业授权,POI开源免费
  2. 对于简单的文档关键字替换使用POI
  3. 复杂的表格编辑,使用Aspose中的标签替换
  4. 对于替换参数(数字,文本,表格,图片等),可封装替换参数,增强代码的适用性

代码示例

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);

问题解决

Linux下精确查找缺失字体

Document document = new Document(inputStream);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(5);

// 输出缺失字体
IWarningCallback callback = new IWarningCallback() {
@Override
	public void warning(WarningInfo info) {
		if (info.getWarningType() == WarningType.FONT_SUBSTITUTION) {
		   System.out.println("AsposeFont: " + info.getDescription());
		}
	}
};
document.setWarningCallback(callback);

document.save(outputStream, pdfSaveOptions);