Remove PDF Pages in Java

The following example shows how to use the Document Editor to produce a new document that doesn’t include pages 1 and 4 from a source document:

DocumentEditor documentEditor = documentToEdit.createDocumentEditor();
Set<Integer> pages = new HashSet<>();
pages.add(1);
pages.add(4);
documentEditor.removePages(pages);
final File file = new File("documentEditorOutput.pdf");
documentEditor.saveDocument(new FileDataProvider(file));

Chaining Edits Together

Document edits can be chained to perform multiple synchronous operations to create a single document. For example:

DocumentEditor documentEditor = documentToEdit.createDocumentEditor();
Set<Integer> pages = new HashSet<>();
pages.add(1);
pages.add(4);
documentEditor.removePages(pages);
Set<Integer> movePages = new HashSet<>();
movePages.add(1);
documentEditor.movePages(movePages, 5, DocumentEditor.IndexPosition.AfterIndex);
final File file = new File("documentEditorOutput.pdf");
documentEditor.saveDocument(new FileDataProvider(file));