Document Editor

With PSPDFKit Libraries, the Document Editor allows you to perform various operations on one or more documents in order to produce a new document. The following sections describe the main editing operations available.

Manipulation of Pages

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

Copy
1
2
3
4
5
6
7
File file = File.createTempFile("documentEditorOutput", ".pdf");
DocumentEditor documentEditor = documentToEdit.getDocumentEditor();
Set<Integer> pages = new HashSet<>();
pages.add(0);
pages.add(2);
documentEditor.keepPages(pages);
documentEditor.saveDocument(new FileDataProvider(file));

This example demonstrates how to remove a set of pages:

Copy
1
2
3
4
5
6
7
File file = File.createTempFile("documentEditorOutput", ".pdf");
DocumentEditor documentEditor = documentToEdit.getDocumentEditor();
Set<Integer> pages = new HashSet<>();
pages.add(1);
pages.add(4);
documentEditor.removePages(pages);
documentEditor.saveDocument(new FileDataProvider(file));

Pages can be moved to a new location in the document. To do this, specify the pages you wish to move and the index they should be moved to:

Copy
1
2
3
4
// Move page 1 after index 5.
Set<Integer> pages = new HashSet<>();
pages.add(1);
documentEditor.movePages(pages, 5, DocumentEditor.IndexPosition.AfterIndex);

New pages can be added to a document. They can be made with different sizing, rotation, insets, and background colors:

Copy
1
2
3
// 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._90, Color.Black, new Insets(0, 0, 0, 0));

All of the events above can be chained to perform multiple synchronous operations to create a single document:

Copy
1
2
3
4
5
6
7
8
9
10
File file = File.createTempFile("documentEditorOutput", ".pdf");
DocumentEditor documentEditor = documentToEdit.getDocumentEditor();
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);
documentEditor.saveDocument(new FileDataProvider(file));

Merging Documents

It’s possible to merge multiple documents together to create one large document.

To do this, we can take one of the documents as a base for the Document Editor and insert the second:

Copy
1
2
3
4
5
6
// Insert the second document before the original and export to the temporary file.
File file = File.createTempFile("documentEditorOutput", ".pdf");
DocumentEditor documentEditor = documentToEdit.getDocumentEditor();
File secondDocument = new File("secondDocument.pdf");
documentEditor.importDocument(0, DocumentEditor.IndexPosition.BeforeIndex, new FileDataProvider(secondDocument));
documentEditor.saveDocument(new FileDataProvider(file));

Changing Page Labels

By default, a PDF’s pages are numbered from 1 to the number of pages contained within it. PDFs often have the page number substituted — or labeled — with some alternative text. For example, the first few pages of a document could be labeled with Roman numerals instead of the usual Western Arabic numerals — e.g. instead of 1, 2, 3, 4, 5, the pages could be labeled I, II, III, IV, V, and then continue on as 6, 7, 8, etc.

After producing a new document by shuffling around, adding, removing, or inserting new pages, it may be necessary to clear the page labels and set new ones.

This example demonstrates how to clear any existing page labels and add custom labels to the first page of the new document:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Create a Document Editor from a source document.
DocumentEditor documentEditor = documentToEdit.getDocumentEditor();

// Clear any existing page labels.
documentEditor.clearPageLabels();

// Set custom labels for the first page.
Set<Integer> pages = new HashSet<>();
pages.add(0);
documentEditor.setPageLabel(pages, "Test");

// Generate a new document.
File file = File.createTempFile("documentEditorOutput", ".pdf");
documentEditor.saveDocument(new FileDataProvider(file));