Add and Insert Images into PDFs on iOS
PSPDFKit for iOS lets you add an image as a new page to a document using Processor or the Document Editor APIs. With this functionality, you can extend your existing PDF documents by adding photos, bitmap graphics, or even images of scanned documents.
If you’d instead like to add an image to an existing PDF page in the form of an annotation, please take a look at the Custom Stamps guide.
Using Processor
To add a new image-based page using the Processor
API, load your document and then assign your desired PDFNewPageConfiguration
to a Processor.Configuration
instance. Processor is a great choice if you want to build an automated document processing operation:
let document = ... guard let configuration = Processor.Configuration(document: document) else { print("Could not create a processor configuration. The document might be locked or invalid.") return } // Add an empty page with the image at the bottom center. let image = UIImage(named: "exampleimage.jpg")! let emptyPageTemplate = PageTemplate.blank let newPageConfiguration = PDFNewPageConfiguration(pageTemplate: emptyPageTemplate) { $0.backgroundColor = UIColor(red: 0.965, green: 0.953, blue: 0.906, alpha: 1) $0.pageMargins = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50) $0.item = ProcessorItem(image: image, jpegCompressionQuality: 0.8) { itemBuilder in itemBuilder.alignment = .bottom itemBuilder.transform = CGAffineTransform(scaleX: 0.3, y: 0.3) } } configuration.addNewPage(at: 0, configuration: newPageConfiguration) let processor = Processor(configuration: configuration, securityOptions: nil) do { // Write the modified document. This can be used to initialize // and present a new PSPDFKit document. try processor.write(toFileURL: destinationURL) } catch { print(error) }
ℹ️ Note: Adding new pages with Processor is only available if you have the Document Editor component enabled in your license.
Using the Document Editor
Image-based pages can also be added using the Document Editor API. The Document Editor is particularly useful if you want to build a general-purpose UI for manipulating pages, as it provides helpers that make building a document editing UI easy.
Here’s how you can use the Document Editor to add a new page configured to show an image:
// Instance method on a `PDFViewController` subclass. @objc private func addImagePage(image: UIImage) { guard let document = document, let editor = PDFDocumentEditor(document: document) else { print("Document editing not available.") return } // Add an empty page with the image at the bottom center. let emptyPageTemplate = PageTemplate.blank let newPageConfiguration = PDFNewPageConfiguration(pageTemplate: emptyPageTemplate) { $0.backgroundColor = UIColor(red: 0.965, green: 0.953, blue: 0.906, alpha: 1) $0.pageMargins = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50) $0.item = ProcessorItem(image: image, jpegCompressionQuality: 0.8) { itemBuilder in itemBuilder.alignment = .bottom itemBuilder.transform = CGAffineTransform(scaleX: 0.3, y: 0.3) } } let destinationRange = NSRange(location: Int(document.pageCount), length: 1) editor.addPages(in: destinationRange, with: newPageConfiguration) editor.save { _, error in if let error = error { print("Error while saving: \(error)") } else { DispatchQueue.main.async { // Reload the document in the UI. self.reloadData() } } } }
Using the Built-In UI
PSPDFKit for iOS comes with a prebuilt user interface for document editing that includes an option for adding new image-based pages. To learn more, check out the Document Editing UI Overview. To learn more about the image picker used in this UI, please consult the Image Picker guide.