Document Editor

With the PSPDFKit Document Editor, you can 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 Editor.Job to produce a new document that keeps only pages 0 and 2 from a source document:

Copy
1
2
3
4
5
6
7
8
9
10
11
// Make a job with a source document as input.
var editorJob = new PSPDFKit.Document.Editor.Job(sourceDocument);

// Keep the first and third pages.
await editorJob.KeepPagesAsync(new int[]{ 0, 2 });

// Generate a new document.
var newDocumentStorageFile = await PSPDFKit.Document.Editor.Editor.NewStorageFileFromJobAsync(editorJob);

// Load this document into a `PdfView`.
PdfView.PdfFileSource = newDocumentStorageFile;

This example demonstrates how to remove a set of pages:

Copy
1
2
3
4
5
6
7
8
// Make a job with a source document as its input.
var editorJob = new PSPDFKit.Document.Editor.Job(sourceDocument);

// Remove the first and third pages.
await editorJob.RemovePagesAsync(new int[] { 0, 2 });

// Generate a new document.
var newDocumentStorageFile = await PSPDFKit.Document.Editor.Editor.NewStorageFileFromJobAsync(editorJob);

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:

1
2
// Move the first two pages to page index 2.
await editorJob.MovePagesAsync(new int[] { 0, 1 }, 2);

Merging Documents and Adding Pages

New pages can be added to a new document from one or more document sources.

This example code is taken from the Catalog example provided with the SDK. It demonstrates how to append one document to another:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Create a job from a source document.
var job = new PSPDFKit.Document.Editor.Job(_documentOne);

// For every page in a second source document, make a new page and add it to the job.
var docOnePageCount = await _documentOne.GetTotalPageCountAsync();
var docTwoPageCount = await _documentTwo.GetTotalPageCountAsync();
for (var pageIndex = 0; pageIndex < docTwoPageCount; pageIndex++ )
{
    // Create a new page from the second document at the page index.
    var newPage = NewPage.FromPage(_documentTwo, pageIndex);

    // Append it to our job's document.
    await job.AddPageAtIndexAsync(newPage, docOnePageCount + pageIndex);
}

// Create a new PDF and get its `StorageFile`.
var newDocument = await Editor.NewStorageFileFromJobAsync(job);

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 possibly also set new ones.

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

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Make a job with a source document as input.
var editorJob = new PSPDFKit.Document.Editor.Job(sourceDocument);

// Clear any existing page labels.
await editorJob.ClearPageLabelsAsync();

// Set our own custom labels for the first three pages.
var customLabels = new string[] { "Front Page", "Page B", "Contents Page" };
var numPages = await editorJob.GetPageCountAsync();
for (var i = 0; i < customLabels.Length && i < numPages; i++)
{
    await editorJob.SetPageLabelAsync(i, customLabels[i]);
}

// Generate a new document.
var newDocumentStorageFile = await PSPDFKit.Document.Editor.Editor.NewStorageFileFromJobAsync(editorJob);