The Facades Layer in Aspose.PDF FOSS for Java

Aspose.PDF FOSS for Java includes a facades layer — a set of high-level, task-focused classes that provide targeted operations on PDF documents without requiring navigation of the full PDF object model. Each facade class follows the same pattern: bind a document, perform operations, save the result.

All facade classes reside in the org.aspose.pdf.facades package:

import org.aspose.pdf.facades.*;

PdfAnnotationEditor: Flatten and Remove Annotations

PdfAnnotationEditor provides operations for managing annotations in bulk: flattening them into the page content, removing all annotations, or removing specific annotation types by name.

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

The deleteAnnotations(annotationType) overload accepts an annotation type string such as "Highlight" or "Widget" to remove only annotations of that type. countAnnotations() returns the total annotation count across all pages.

PdfBookmarkEditor: Create and Delete Bookmarks

PdfBookmarkEditor manages the PDF outline tree (bookmarks). It exposes methods for creating new bookmarks with createBookmarks(), deleting all bookmarks with deleteBookmarks(), and removing bookmarks by title with deleteBookmarkByTitle().

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

The bindPdf(document) overload accepts a Document object directly, enabling use in pipelines where the document is already loaded into memory.

PdfContentEditor: Text Replacement

PdfContentEditor supports targeted text replacement within PDF content streams. replaceText(searchText, replaceText) substitutes all occurrences of a string across the entire document; replaceText(searchText, pageNumber, replaceText) limits the replacement to a single page.

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

TextReplaceOptions controls whether the replacement should preserve the original font and size or inherit text properties from the surrounding content. getStamps(pageNumber) returns StampInfo objects for all stamps on a page, and deleteStampById() removes a specific stamp by its identifier.

PdfFileSecurity: Encryption and Permissions

PdfFileSecurity manages document-level security including encryption, decryption, and access permissions. The constructor variants accept file paths, streams, or an existing Document instance alongside the output destination.

DocumentPrivilege represents the access permissions for the encrypted document. Use DocumentPrivilege.getForbidAll() to deny all user operations, or DocumentPrivilege.getAllowAll() to grant full access.

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) removes encryption when the owner password is known. The Algorithm enum provides RC4 and AES algorithm families; KeySize specifies the key length.

PdfFileStamp: Adding Stamps and Page Numbers

PdfFileStamp adds text and image stamps to PDF pages and can automatically generate formatted page numbers. Stamps are positioned using coordinate-based placement on each page.

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: Text and Image Extraction

PdfExtractor provides a pull-based API for extracting text and images from PDF documents. After binding a document, call extractText() to trigger text extraction, then iterate with hasNextPageText() and getNextPageText():

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

For image extraction, extractImage() triggers the extraction and hasNextImage() / getNextImage(outputStream) iterate over the extracted images. The getPassword() and setPassword() methods support encrypted documents.

Getting Started

Add the Maven dependency:

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-pdf</artifactId>
  <version>0.1.0-alpha</version>
</dependency>

All facade classes require Java 11 or later. Aspose.PDF FOSS for Java is MIT-licensed and available at https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Java.