Add Pages to PDFs in Java

The Document Editor allows you to add new pages to a document. They can be made with different sizing, rotation, insets, and background colors:

File file = File.createTempFile("documentEditorOutput", ".pdf");
DocumentEditor documentEditor = documentToEdit.createDocumentEditor();
// Add a new page before index 0 with a size of 200x200 and a background of black.
documentEditor.AddPage(0, DocumentEditor.IndexPosition.BeforeIndex, 200, 200,
                       Rotation.Degrees90, Color.black, new Insets(0, 0, 0, 0));
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:

File file = File.createTempFile("documentEditorOutput", ".pdf");
DocumentEditor documentEditor = documentToEdit.createDocumentEditor();
documentEditor.AddPage(0, DocumentEditor.IndexPosition.BeforeIndex, 200, 200,
                       Rotation.Degrees90, Color.black, new Insets(0, 0, 0, 0));
Set<Integer> pages = new HashSet<>();
pages.add(1);
pages.add(4);
documentEditor.removePages(pages);
documentEditor.saveDocument(new FileDataProvider(file));