Merge Multiple PDF Files on iOS
PSPDFKit allows you to instantiate a Document
with multiple files, and you can use the Processor
API to merge multiple PDF files into a single one. To achieve this, initialize a document with multiple data providers using init(dataProviders:)
, and then use the Processor
API to write the merged PDF to disk:
// Create `FileDataProvider` number one with a URL that points to a PDF. let fileProvider1 = FileDataProvider(fileURL: writableURL1) // Create `FileDataProvider` number two with a URL that points to a PDF. let fileProvider2 = FileDataProvider(fileURL: writableURL2) // Initialize the `Document` with an array of data providers. let multipleFilesDocument = Document(dataProviders:[fileProvider1, fileProvider2]) // Create a default configuration. let configuration = Processor.Configuration(document: multipleFilesDocument)! // Initialize the processor. let processor = Processor(configuration: configuration, securityOptions: nil) // Write the merged document to disk. try processor.write(toFileURL: mergedDocumentURL) // Use the merged document. let mergedDocument = Document(url: mergedDocumentURL)
// Create `PSPDFFileDataProvider` number one with a URL that points to a PDF. PSPDFFileDataProvider *fileProvider1 = [[PSPDFFileDataProvider alloc] initWithFileURL:writableURL1]; // Create `PSPDFFileDataProvider` number two with a URL that points to a PDF. PSPDFFileDataProvider *fileProvider2 = [[PSPDFFileDataProvider alloc] initWithFileURL:writableURL2]; // Initialize the `PSPDFDocument` with an array of data providers. PSPDFDocument *multipleFilesDocument = [[PSPDFDocument alloc] initWithDataProviders:@[fileProvider1, fileProvider2]]; // Create a default configuration. PSPDFProcessorConfiguration *configuration = [[PSPDFProcessorConfiguration alloc] initWithDocument:multipleFilesDocument]; // Initialize the processor. PSPDFProcessor *processor = [[PSPDFProcessor alloc] initWithConfiguration:configuration securityOptions:nil]; // Write the merged document to disk. [processor writeToFileURL:mergedDocumentURL error:NULL]; // Use the merged document. PSPDFDocument *mergedDocument = [[PSPDFDocument alloc] initWithURL:mergedDocumentURL];
The sample code above allows you to merge all the pages from the two documents. The order of the file data providers is important.
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];
You can also use Processor.Configuration.merge(item:onPage:)
to merge (add) the image of a PDF page into the specified document page.
Similar functionality is also available via the Document Editor. Please see the PDFDocumentEditor.importPages(to:, from:, withCompletionBlock:, queue:)
API documentation for more information.