Aspose.PDF FOSS 中针对 Java 的 Facades 层

Aspose.PDF FOSS for Java 包含一个 facades 层 —— 一组高层次、任务导向的类,提供对 PDF 文档的特定操作,无需遍历完整的 PDF 对象模型。每个 facade 类遵循相同的模式:绑定文档,执行操作,保存结果。

所有 facade 类都位于 org.aspose.pdf.facades 包中:

import org.aspose.pdf.facades.*;

PdfAnnotationEditor:扁平化和删除批注

PdfAnnotationEditor 提供批量管理批注的操作:将批注扁平化为页面内容,删除所有批注,或按名称删除特定类型的批注。

try (PdfAnnotationEditor editor = new PdfAnnotationEditor()) {
    editor.bindPdf("annotated.pdf");
    // Flatten all annotations into the page content stream
    editor.flattenAnnotations();
    editor.save("flat.pdf");
}

deleteAnnotations(annotationType) 的重载接受批注类型字符串,例如 "Highlight""Widget",仅删除该类型的批注。countAnnotations() 返回所有页面的批注总数。

PdfBookmarkEditor:创建和删除书签

PdfBookmarkEditor 管理 PDF 大纲树(书签)。它提供使用 createBookmarks() 创建新书签的方法,使用 deleteBookmarks() 删除所有书签,以及使用 deleteBookmarkByTitle() 按标题移除书签。

try (PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor()) {
    bookmarkEditor.bindPdf("input.pdf");
    // Remove all bookmarks from the document
    bookmarkEditor.deleteBookmarks();
    bookmarkEditor.save("no-bookmarks.pdf");
}

bindPdf(document) 的重载直接接受一个 Document 对象,使其能够在文档已加载到内存的流水线中使用。

PdfContentEditor:文本替换

PdfContentEditor 支持在 PDF 内容流中进行有针对性的文本替换。replaceText(searchText, replaceText) 替换整个文档中出现的所有字符串;replaceText(searchText, pageNumber, replaceText) 将替换限制在单个页面内。

try (PdfContentEditor contentEditor = new PdfContentEditor()) {
    contentEditor.bindPdf("template.pdf");
    contentEditor.replaceText("{{CompanyName}}", "Acme Corp");
    contentEditor.save("output.pdf");
}

TextReplaceOptions 控制替换是应保留原始字体和大小,还是从周围内容继承文本属性。getStamps(pageNumber) 返回页面上所有印章的 StampInfo 对象,deleteStampById() 则通过标识符移除特定的印章。

PdfFileSecurity:加密与权限

PdfFileSecurity 管理文档级安全,包括加密、解密和访问权限。构造函数的不同变体接受文件路径、流或现有的 Document 实例以及输出目标。

DocumentPrivilege 表示加密文档的访问权限。使用 DocumentPrivilege.getForbidAll() 拒绝所有用户操作,或使用 DocumentPrivilege.getAllowAll() 授予完整访问权限。

try (PdfFileSecurity security = new PdfFileSecurity("input.pdf", "secured.pdf")) {
    DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
    privilege.setAllowPrint(true);
    security.encryptFile("userPass", "ownerPass", privilege, KeySize.x256);
}

decryptFile(ownerPassword) 在已知所有者密码时移除加密。Algorithm 枚举提供 RC4AES 算法族;KeySize 指定密钥长度。

PdfFileStamp:添加印章和页码

PdfFileStamp 向 PDF 页面添加文本和图像印章,并且可以自动生成格式化的页码。印章通过基于坐标的方式在每页上定位。

try (PdfFileStamp stamp = new PdfFileStamp("input.pdf", "stamped.pdf")) {
    // Add formatted page numbers to all pages
    stamp.addPageNumber("Page #", PageNumberingStyle.NumeralsArabic,
        0, 0, 0, 20);
}

PdfExtractor:文本与图像提取

PdfExtractor 提供基于拉取的 API 用于从 PDF 文档中提取文本和图像。绑定文档后,调用 extractText() 触发文本提取,然后使用 hasNextPageText()getNextPageText() 进行迭代:

try (PdfExtractor extractor = new PdfExtractor()) {
    extractor.bindPdf("document.pdf");
    extractor.extractText();
    while (extractor.hasNextPageText()) {
        String pageText = extractor.getNextPageText();
        // Collect extracted page content
    }
}

对于图像提取,extractImage() 触发提取,hasNextImage() / getNextImage(outputStream) 对提取的图像进行迭代。getPassword()setPassword() 方法支持加密文档。

快速入门

添加 Maven 依赖:

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-pdf-foss</artifactId>
  <version>26.8.0</version>
</dependency>

所有外观类都需要 Java 11 或更高版本。Aspose.PDF FOSS 用于 Java 是 MIT 许可证的,并可在 https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Java 获取。

相关资源