How to Work with PDF Facades in Java
Flatten Annotations
Flatten all annotations into the page content stream so they are no longer editable or removable as separate annotation objects. This is a common step before printing or archiving a document:
try (PdfAnnotationEditor editor = new PdfAnnotationEditor()) {
editor.bindPdf("annotated.pdf");
editor.flattenAnnotations();
editor.save("flat.pdf");
}Remove Bookmarks
Delete all outline bookmarks from a document. Bookmarks (also called outlines) are the navigation tree entries visible in PDF reader sidebars:
try (PdfBookmarkEditor editor = new PdfBookmarkEditor()) {
editor.bindPdf("input.pdf");
editor.deleteBookmarks();
editor.save("no-bookmarks.pdf");
}Replace Text
Substitute a string in all pages of a document. This is useful for filling in template placeholders before distributing a document:
try (PdfContentEditor editor = new PdfContentEditor()) {
editor.bindPdf("template.pdf");
editor.replaceText("{{Date}}", "2026-05-24");
editor.save("output.pdf");
}Encrypt a Document
Apply AES-256 encryption with access permissions using PdfFileSecurity. The
DocumentPrivilege.getForbidAll() preset disables all operations; individual permissions
can then be re-enabled with setter methods:
try (PdfFileSecurity security = new PdfFileSecurity("input.pdf", "secured.pdf")) {
DocumentPrivilege priv = DocumentPrivilege.getForbidAll();
priv.setAllowPrint(true);
security.encryptFile("user", "owner", priv, KeySize.x256);
}Extract Text
Iterate over page text using PdfExtractor. Call extractText() first to process
all pages, then retrieve each page’s text with getNextPageText():
try (PdfExtractor extractor = new PdfExtractor()) {
extractor.bindPdf("document.pdf");
extractor.extractText();
while (extractor.hasNextPageText()) {
String text = extractor.getNextPageText();
System.out.println(text);
}
}