Swift PDF Tutorial: Create, View, and Edit Documents Easily Managing PDF files is a core requirement for many iOS applications. Apple provides a powerful, native framework called PDFKit that makes handling these documents straightforward. This tutorial covers the essentials of creating, viewing, and modifying PDF files natively in Swift. Setting Up PDFKit
To use Apple’s native PDF tools, you must import the PDFKit framework at the top of your Swift file. import UIKit import PDFKit Use code with caution. 1. How to View a PDF Document
The easiest way to display a PDF is by using PDFView. This view handles rendering, zooming, and scrolling automatically. Step-by-Step Implementation: Create a PDFView instance. Load the PDF file from your local bundle or a URL.
Assign the document to the view and add it to your screen layout.
class PDFViewerViewController: UIViewController { let pdfView = PDFView() override func viewDidLoad() { super.viewDidLoad() // 1. Configure PDFView layout pdfView.frame = self.view.bounds pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight] self.view.addSubview(pdfView) // 2. Load the PDF document if let sampleURL = Bundle.main.url(forResource: “sample”, withExtension: “pdf”) { if let document = PDFDocument(url: sampleURL) { // 3. Display the document pdfView.document = document pdfView.autoScales = true } } } } Use code with caution. 2. How to Create a PDF Document
You can generate brand new PDF files programmatically by drawing graphics and text into a PDF graphics context. Step-by-Step Implementation:
Define the page size (standard US Letter size is 612 × 792 points). Initialize a UIGraphicsPDFRenderer.
Draw your text, shapes, or images inside the renderer block. Save the generated data to a file.
func createSimplePDF() -> Data { // Define page dimensions (Letter size) let pageRect = CGRect(x: 0, y: 0, width: 612, height: 792) let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: UIGraphicsPDFRendererFormat()) let pdfData = renderer.pdfData { (context) in // Start a new page context.beginPage() // Define text attributes let attributes: [NSAttributedString.Key: Any] = [ .font: UIFont.boldSystemFont(ofSize: 24), .foregroundColor: UIColor.black ] let text = “Hello, Swift PDFKit!” let textRect = CGRect(x: 50, y: 50, width: 500, height: 40) // Render the text onto the PDF page text.draw(in: textRect, withAttributes: attributes) } return pdfData } Use code with caution. 3. How to Edit a PDF Document
Editing PDFs usually involves adding new pages, rearranging existing pages, or overlaying annotations like text highlights and signatures. Example: Adding a Blank Page to an Existing PDF
func insertBlankPage(to document: PDFDocument, at index: Int) { // Create a new blank page with standard dimensions let pageSize = CGRect(x: 0, y: 0, width: 612, height: 792) if let newPage = PDFPage() { newPage.setBounds(pageSize, for: .mediaBox) // Insert the page into the document document.insert(newPage, at: index) } } Use code with caution. Example: Adding a Highlight Annotation
Annotations allow you to draw or place interactive elements on top of the existing document layer.
func addHighlightToFirstPage(document: PDFDocument) { guard let firstPage = document.page(at: 0) else { return } // Define the bounding box for the highlight let highlightBounds = CGRect(x: 50, y: 700, width: 200, height: 30) // Create a highlight annotation let highlight = PDFAnnotation(bounds: highlightBounds, forType: .highlight, withProperties: nil) highlight.color = UIColor.yellow.withAlphaComponent(0.5) // Add the annotation to the page firstPage.addAnnotation(highlight) } Use code with caution. Summary of Core Classes PDFView: The visual UI component used to display documents.
PDFDocument: The data object that represents the PDF file itself. PDFPage: Represents an individual page inside a document.
PDFAnnotation: Items placed on a page, such as notes, links, highlights, and forms.
Show me how to add a digital signatureShow me how to add a digital signature
Show me how to extract text from a PDFShow me how to extract text from a PDF
Show me how to build a thumbnail grid navigationShow me how to build a thumbnail grid navigation Saved time Comprehensive Inappropriate Not working
A copy of this chat, including the images and video, will be included with your feedback A copy of this chat will be included with your feedback
Your feedback will include a copy of this chat and the image from your search
Your feedback will include a copy of this chat, any links you shared, and the image from your search.
Thanks for letting us know
Google may use account and system data to understand your feedback and improve our services, subject to our Privacy Policy and Terms of Service. For legal issues, make a legal removal request.