Duplicate PDF pages in Java
The following example shows how to duplicate pages using the Document Editor.
Duplicating pages
Duplicating pages is done using the DocumentEditor.duplicatePages
method, which takes a list of page indices to duplicate:
final DocumentEditor documentEditor = documentToEdit.createDocumentEditor();final Set<Integer> pages = new HashSet<>();pages.add(0);documentEditor.duplicatePages(pages);final File file = new File("documentEditorOutput.pdf");documentEditor.saveDocument(new FileDataProvider(file));
This will create a copy of the first page of a document and place it directly after the first page.
Chaining operations
It’s also possible to chain editor operations. This can be done either by using a JSON operations array or by calling multiple Document Editor methods before saveDocument
:
final Set<Integer> pages = new HashSet<>();pages.add(2);documentEditor.duplicatePages(pages);pages.add(0);documentEditor.rotatePages(pages, Rotation.Degrees90);documentEditor.saveDocument(new FileDataProvider(file));