Extract Pages from PDFs in Java

The following example shows how to use the Document Editor to extract pages 0 and 2 from a source document to produce a new document:

File file = File.createTempFile("documentEditorOutput", ".pdf");
DocumentEditor documentEditor = documentToEdit.createDocumentEditor();
Set<Integer> pages = new HashSet<>();
pages.add(0);
pages.add(2);
documentEditor.keepPages(pages);
documentEditor.saveDocument(new FileDataProvider(file));

Chaining Edits Together

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

File file = File.createTempFile("documentEditorOutput", ".pdf");
DocumentEditor documentEditor = documentToEdit.createDocumentEditor();
Set<Integer> pages = new HashSet<>();
pages.add(0);
pages.add(2);
documentEditor.keepPages(pages);
Set<Integer> movePages = new HashSet<>();
movePages.add(0);
documentEditor.movePages(movePages, 1, DocumentEditor.IndexPosition.AfterIndex);
documentEditor.saveDocument(new FileDataProvider(file));