Add pages to PDF files on iOS

With Nutrient, there are three ways to add a page to a document:

  1. Add a blank page programmatically
  2. Take a page from a different document and insert it as a new page programmatically
  3. Interactively with the user interface

Adding a blank page

You can add a blank page to a document using the .blank template document. This is how it looks in code:

// Create a default configuration.
let processorConfiguration = Processor.Configuration(document: firstDocument)!
// Create a blank page template.
let pageTemplate = PageTemplate(pageType: .emptyPage, identifier: .blank)
// Create a new page configuration.
let pageConfiguration = PDFNewPageConfiguration(pageTemplate: pageTemplate, builderBlock: nil)
// Add the first page from the second document as the second page (index 1) of the resulting merged document.
processorConfiguration.addNewPage(at: 1, configuration: PDFNewPageConfiguration(pageTemplate: pageTemplate, builderBlock: nil))
// Initialize the processor.
let processor = Processor(configuration: configuration, securityOptions: nil)
// Write the merged document to disk. Alternatively, you can use `output(to:)`
// to save the resulting document to a custom data source.
try processor.write(toFileURL: mergedDocumentURL)
// Use the merged document.
let mergedDocument = Document(url: mergedDocumentURL)

Inserting a page from another document

If you’re looking to merge or add specific pages from one document to another, you can use Processor.Configuration.addNewPage(at:configuration:). The example below inserts the first page from the second document as the second page of the resulting merged document:

// Create a default configuration.
let processorConfiguration = Processor.Configuration(document: firstDocument)!
// Create a page template with the first page of the second document.
let pageTemplate = PageTemplate(document: secondDocument, sourcePageIndex: 0)
// Create a new page configuration.
let pageConfiguration = PDFNewPageConfiguration(pageTemplate: pageTemplate, builderBlock: nil)
// Add the first page from the second document as the second page (index 1) of the resulting merged document.
processorConfiguration.addNewPage(at: 1, configuration: PDFNewPageConfiguration(pageTemplate: pageTemplate, builderBlock: nil))
// Initialize the processor.
let processor = Processor(configuration: configuration, securityOptions: nil)
// Write the merged document to disk. Alternatively, you can use `output(to:)`
// to save the resulting document to a custom data source.
try processor.write(toFileURL: mergedDocumentURL)
// Use the merged document.
let mergedDocument = Document(url: mergedDocumentURL)

Using the user interface

The Document Editor component offers a convenient UI for inserting blank pages or pages with predefined patterns into existing documents — this is perfect for adding new pages for more scratch space for drawing or adding textual notes.