Add pages to PDF files on iOS
With Nutrient, there are three ways to add a page to a document:
- Add a blank page programmatically
- Take a page from a different document and insert it as a new page programmatically
- 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)
// Create a default configuration.PSPDFProcessorConfiguration *processorConfiguration = [[PSPDFProcessorConfiguration alloc] initWithDocument:firstDocument];// Create a blank page template.PSPDFPageTemplate *pageTemplate = [[PSPDFPageTemplate alloc] initWithPageType:PSPDFNewPageTypeEmptyPage identifier:PSPDFTemplateIdentifierBlank];// Create a new page configuration.PSPDFNewPageConfiguration *pageConfiguration = [PSPDFNewPageConfiguration newPageConfigurationWithPageTemplate:pageTemplate builderBlock:NULL];// Add the first page from the second document as the second page (index 1) of the resulting merged document.[processorConfiguration addNewPageAtIndex:1 configuration:pageConfiguration];// Initialize the processor.PSPDFProcessor *processor = [[PSPDFProcessor alloc] initWithConfiguration:processorConfiguration securityOptions:nil];// Write the merged document to disk. Alternatively, you can use `outputToDataSink:error:`// to save the resulting document to a custom data source.[processor writeToFileURL:mergedDocumentURL error:NULL];// Use the merged document.PSPDFDocument *mergedDocument = [[PSPDFDocument alloc] initWithURL: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)
// Create a default configuration.PSPDFProcessorConfiguration *processorConfiguration = [[PSPDFProcessorConfiguration alloc] initWithDocument:firstDocument];// Create a page template with the first page of the second document.PSPDFPageTemplate *pageTemplate = [[PSPDFPageTemplate alloc] initWithDocument:secondDocument sourcePageIndex:0];// Create a new page configuration.PSPDFNewPageConfiguration *pageConfiguration = [PSPDFNewPageConfiguration newPageConfigurationWithPageTemplate:pageTemplate builderBlock:NULL];// Add the first page from the second document as the second page (index 1) of the resulting merged document.[processorConfiguration addNewPageAtIndex:1 configuration:pageConfiguration];// Initialize the processor.PSPDFProcessor *processor = [[PSPDFProcessor alloc] initWithConfiguration:processorConfiguration securityOptions:nil];// Write the merged document to disk. Alternatively, you can use `outputToDataSink:error:`// to save the resulting document to a custom data source.[processor writeToFileURL:mergedDocumentURL error:NULL];// Use the merged document.PSPDFDocument *mergedDocument = [[PSPDFDocument alloc] initWithURL: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.