Custom Page Templates

Starting with PSPDFKit 7.6 for iOS, you can customize the set of templates that PSPDFNewPageViewController offers on the picker via the new PSPDFPageTemplate API.

The page templates can be updated via the pageTemplates property on PSPDFDocumentEditorConfiguration. Then you can instantiate a PSPDFNewPageViewController with that configuration. If you wish to make your custom set of templates available globally, you can update the page templates list on a PSPDFDocumentEditorConfiguration and then assign that configuration to -[PSPDFConfiguration documentEditorConfiguration].

Removing PSPDFKit-Provided Templates

PSPDFKit ships with a predefined list of page templates, which are ready to use:

  • Blank
  • Line 5mm
  • Dot 5mm
  • Line 7mm

When you don’t want some of these default templates to be available as options for creating or editing PDF documents, you can use -[PSPDFDocumentEditorConfiguration pageTemplates]:

Copy
1
2
3
4
5
6
7
let documentEditorConfiguration = PSPDFDocumentEditorConfiguration { builder in
    builder.pageTemplates = []
}
let configuration = PSPDFConfiguration { builder in
    builder.documentEditorConfiguration = documentEditorConfiguration
}
let controller = PSPDFViewController(document: document, configuration: configuration)
Copy
1
2
3
4
5
6
7
PSPDFDocumentEditorConfiguration *documentEditorConfiguration = [PSPDFDocumentEditorConfiguration configurationWithBuilder:^(PSPDFDocumentEditorConfigurationBuilder *builder) {
    builder.pageTemplates = @[]
}];
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
    builder.documentEditorConfiguration = documentEditorConfiguration
}];
PSPDFViewController *controller = [[PSPDFViewController alloc] initWithDocument:document configuration:configuration];

The PSPDFPageTemplate Class

You can use the PSPDFPageTemplate class to build page templates from your own PDF documents.

PSPDFPageTemplate has two initializers you can use depending on what you’re trying to achieve:

Please note: Creating a tiled pattern page template requires the source document to be exported correctly. This means that the source PDF needs to contain a pattern itself. If a PSPDFPageTemplate is instantiated using the tiled pattern initializer and the source document does not contain a pattern, the rendering will fail silently.

Check out DocumentEditorCustomTemplates.swift in the Catalog to see PSPDFPageTemplate in action.

Custom Tiled Templates

Many uses for page templates require the background being tiled (or patterned).

A page is considered tiled if there are one or more images repeated on the page.

For a PDF to be able to work as a source for a tiled page template using the new -[PSPDFPageTemplate initWithTiledPatternFromDocument:sourcePageIndex:] API, it has to have actual pattern path information embedded.

To accomplish this, you can use Adobe Illustrator or any other vector editing tool.

When creating your own patterns, please take the following points into consideration:

  1. What’s rendered on the page is the path information embedded in the PDF and not the actual PDF.
  2. If your custom pattern needs certain spacing between tiles, that information needs to be included within the pattern information as well. Currently, there’s no way to specify spacing between tiles from the PSPDFKit API.

Click here to download a custom sample template.

After you’ve created the PDF document that will serve as a pattern source, you can use it with PSPDFKit as follows:

Copy
1
2
3
4
5
6
7
8
9
10
let document = PSPDFDocument(url: url)
let customTemplate = PSPDFPageTemplate(tiledPatternFrom: document, sourcePageIndex: 0)

let editorConfiguration = PSPDFDocumentEditorConfiguration { (builder) in
  builder.pageTemplates.append(contentsOf: [customTemplate])
}

let newPageViewController = PSPDFNewPageViewController(documentEditorConfiguration: editorConfiguration)

present(newPageViewController, options: options, animated: true, sender: sender)