Extract Pages from PDFs on iOS

PSPDFKit’s Processor can extract pages from one document and put them in another document. You can choose to extract a single page, a range of pages, or even multiple page ranges.

In most cases, you’ll want to use Processor to create a new PDF document on disk based on a current Document. The example below shows how to flatten annotations and extract the first three pages in a document to another document on disk:

// Create a default configuration.
let configuration = Processor.Configuration(document: document)!
// Change all annotations to be flattened (instead of saved as annotations).
configuration.modifyAnnotations(ofTypes: .all, change: .flatten)
// Only extract pages 0, 1, and 2.
configuration.includeOnlyIndexes(IndexSet(integersIn: 0...2))

// Start the conversion from `document` to `extractedURL`.
let processor = Processor(configuration: configuration, securityOptions: nil)
// Save to a new file on disk. Alternatively, you can use `output(to:)`
// to save the resulting document to a custom data source.
try processor.write(toFileURL: extractedURL)
// Create a default configuration.
PSPDFProcessorConfiguration *configuration = [[PSPDFProcessorConfiguration alloc] initWithDocument:document];
// Change all annotations to be flattened (instead of saved as annotations).
[configuration modifyAnnotationsOfTypes:PSPDFAnnotationTypeAll change:PSPDFAnnotationChangeFlatten];
// Only extract pages 0, 1, and 2.
[configuration includeOnlyIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]];

// Start the conversion from `document` to `extractedURL`.
PSPDFProcessor *processor = [[PSPDFProcessor alloc] initWithConfiguration:configuration securityOptions:nil];
// Save to a new file on disk. Alternatively, you can use `outputToDataSink:error:`
// to save the resulting document to a custom data source.
[processor writeToFileURL:extractedURL error:NULL];

Similar functionality is also available via the Document Editor. Please see the PDFDocumentEditor.exportPages(_,to:, withCompletionBlock:) API documentation for more information.