PDF Facades

This guide shows how to use the task-focused facade classes in the org.aspose.pdf.facades package. Each facade follows the same lifecycle: bindPdf() → operations → save(). You will learn to flatten annotations, manage bookmarks, replace text, encrypt documents, extract text, and add page numbers.

Facade Pattern

All facade classes in org.aspose.pdf.facades follow the same lifecycle: bindPdf() → operations → save(). They accept file paths, streams, or Document objects.

PdfAnnotationEditor

Flatten, delete, or count annotations across the entire document. flattenAnnotations() merges all annotations into the page content stream, making them permanent and non-editable:

try (PdfAnnotationEditor editor = new PdfAnnotationEditor()) {
    editor.bindPdf("input.pdf");
    editor.flattenAnnotations();
    editor.save("output.pdf");
}

deleteAnnotations(annotationType) removes a specific annotation type by name.

PdfBookmarkEditor

Create and delete PDF outline entries (bookmarks). deleteBookmarks() removes all outline entries from the bound document before saving:

try (PdfBookmarkEditor editor = new PdfBookmarkEditor()) {
    editor.bindPdf("input.pdf");
    editor.deleteBookmarks();
    editor.save("output.pdf");
}

PdfContentEditor

Replace text in PDF content streams across the whole document or on a specific page. replaceText(searchString, replacement) finds and replaces all matching text strings:

try (PdfContentEditor editor = new PdfContentEditor()) {
    editor.bindPdf("template.pdf");
    editor.replaceText("{{Name}}", "John Smith");
    editor.save("output.pdf");
}

PdfFileSecurity

Encrypt a document with AES-256 and set access permissions via DocumentPrivilege. encryptFile(userPass, ownerPass, privilege, keySize) applies encryption and writes the result to the configured output file:

try (PdfFileSecurity security = new PdfFileSecurity("input.pdf", "out.pdf")) {
    DocumentPrivilege priv = DocumentPrivilege.getForbidAll();
    priv.setAllowPrint(true);
    security.encryptFile("user", "owner", priv, KeySize.x256);
}

PdfExtractor

Extract text page by page from a PDF document. After calling extractText(), use hasNextPageText() and getNextPageText() to iterate each page’s extracted content:

try (PdfExtractor extractor = new PdfExtractor()) {
    extractor.bindPdf("document.pdf");
    extractor.extractText();
    while (extractor.hasNextPageText()) {
        String text = extractor.getNextPageText();
        System.out.println(text);
    }
}

PdfFileStamp

Add page number stamps to PDF pages using addPageNumber(FormattedText). Pass a FormattedText instance containing the page number label:

try (PdfFileStamp stamp = new PdfFileStamp("in.pdf", "out.pdf")) {
    stamp.addPageNumber(new FormattedText("Page #"));
    stamp.close();
}

Use addHeader(FormattedText, float) or addFooter(FormattedText, float) to add recurring text at a specified margin from the top or bottom of every page.

See Also

 English