PDFKit
Display, navigate, search, annotate, and manipulate PDF documents with PDFView, PDFDocument, PDFPage, PDFAnnotation, and PDFSelection.
Contents
Workflow
- Load the document safely, handle password/invalid data, and define who owns mutations and saves.
- Configure
PDFView display, scaling, navigation, and observation for the actual product surface.
- Perform search, selection, annotation, form, thumbnail, or page operations with PDF coordinate conversion in mind.
- Keep SwiftUI representable identity stable and strongly own weak delegates/providers.
- Save to a deliberate destination and verify reload, rotation, large files, protected files, and annotation persistence.
Route by Task
- Read core implementation details for loading, viewing, navigation, search, annotations, thumbnails, SwiftUI integration, and page overlays.
- Read PDF generation and forms for forms, watermarks, merging, and printing recipes.
- Read PDF viewing and annotations for outlines, custom drawing, and overlay lifecycle recipes.
Core Decisions
- Never assume
PDFDocument initialization succeeds or that a protected document is unlocked.
- Convert between view and page coordinates before placing or hit-testing annotations.
- Keep PDF mutations serialized and update UI-facing state on the main actor.
- Avoid representable update loops caused by comparing document objects or rebuilding the view.
Common Mistakes
DON'T: Force-unwrap PDFDocument init
PDFDocument(url:) and PDFDocument(data:) are failable initializers.
// WRONG
let document = PDFDocument(url: url)!
// CORRECT
guard let document = PDFDocument(url: url) else { return }
DON'T: Forget autoScales on PDFView
Without autoScales, the PDF renders at its native resolution.
// WRONG
pdfView.document = document
// CORRECT
pdfView.autoScales = true
pdfView.document = document
DON'T: Ignore PDF coordinate system in annotations
PDF page coordinates have origin at the bottom-left with Y increasing
upward -- opposite of UIKit.
// WRONG: UIKit coordinates
let bounds = CGRect(x: 50, y: 50, width: 200, height: 30)
// CORRECT: PDF coordinates (origin bottom-left)
let pageBounds = page.bounds(for: .mediaBox)
let pdfY = pageBounds.height - 50 - 30
let bounds = CGRect(x: 50, y: pdfY, width: 200, height: 30)
DON'T: Modify annotations on a background thread
PDFKit classes are not thread-safe. All mutations must occur on @MainActor.
// WRONG: Modifying PDFKit objects from background tasks
Task.detached { page.addAnnotation(annotation) }
// CORRECT: Perform mutations on @MainActor
@MainActor
func addNote(to page: PDFPage, annotation: PDFAnnotation) {
page.addAnnotation(annotation)
}
DON'T: Compare PDFDocument with == in UIViewRepresentable
PDFDocument is a reference type. Use identity (!==).
// WRONG: Always replaces document
func updateUIView(_ pdfView: PDFView, context: Context) {
pdfView.document = document
}
// CORRECT
func updateUIView(_ pdfView: PDFView, context: Context) {
if pdfView.document !== document {
pdfView.document = document
}
}
Review Checklist
References
1---2name: pdfkit3description: Display and manipulate PDF documents using PDFKit. Use when embedding PDFView to show PDF files, creating or modifying PDFDocument instances, adding annotations (highlights, notes, signature widgets), extracting text with PDFSelection, navigating pages, generating thumbnails, filling PDF forms, or wrapping PDFView in SwiftUI.4---56# PDFKit78Display, navigate, search, annotate, and manipulate PDF documents with `PDFView`, `PDFDocument`, `PDFPage`, `PDFAnnotation`, and `PDFSelection`.910## Contents1112- [Workflow](#workflow)13- [Route by Task](#route-by-task)14- [Core Decisions](#core-decisions)15- [Common Mistakes](#common-mistakes)16- [Review Checklist](#review-checklist)17- [References](#references)1819## Workflow20211. Load the document safely, handle password/invalid data, and define who owns mutations and saves.222. Configure `PDFView` display, scaling, navigation, and observation for the actual product surface.233. Perform search, selection, annotation, form, thumbnail, or page operations with PDF coordinate conversion in mind.244. Keep SwiftUI representable identity stable and strongly own weak delegates/providers.255. Save to a deliberate destination and verify reload, rotation, large files, protected files, and annotation persistence.2627## Route by Task2829- Read [core implementation details](references/core-implementation.md) for loading, viewing, navigation, search, annotations, thumbnails, SwiftUI integration, and page overlays.30- Read [PDF generation and forms](references/pdf-generation-and-forms.md) for forms, watermarks, merging, and printing recipes.31- Read [PDF viewing and annotations](references/pdf-viewing-and-annotations.md) for outlines, custom drawing, and overlay lifecycle recipes.3233## Core Decisions3435- Never assume `PDFDocument` initialization succeeds or that a protected document is unlocked.36- Convert between view and page coordinates before placing or hit-testing annotations.37- Keep PDF mutations serialized and update UI-facing state on the main actor.38- Avoid representable update loops caused by comparing document objects or rebuilding the view.3940## Common Mistakes4142### DON'T: Force-unwrap PDFDocument init4344`PDFDocument(url:)` and `PDFDocument(data:)` are failable initializers.4546```swift47// WRONG48let document = PDFDocument(url: url)!4950// CORRECT51guard let document = PDFDocument(url: url) else { return }52```5354### DON'T: Forget autoScales on PDFView5556Without `autoScales`, the PDF renders at its native resolution.5758```swift59// WRONG60pdfView.document = document6162// CORRECT63pdfView.autoScales = true64pdfView.document = document65```6667### DON'T: Ignore PDF coordinate system in annotations6869PDF page coordinates have origin at the bottom-left with Y increasing70upward -- opposite of UIKit.7172```swift73// WRONG: UIKit coordinates74let bounds = CGRect(x: 50, y: 50, width: 200, height: 30)7576// CORRECT: PDF coordinates (origin bottom-left)77let pageBounds = page.bounds(for: .mediaBox)78let pdfY = pageBounds.height - 50 - 3079let bounds = CGRect(x: 50, y: pdfY, width: 200, height: 30)80```8182### DON'T: Modify annotations on a background thread8384PDFKit classes are not thread-safe. All mutations must occur on `@MainActor`.8586```swift87// WRONG: Modifying PDFKit objects from background tasks88Task.detached { page.addAnnotation(annotation) }8990// CORRECT: Perform mutations on @MainActor91@MainActor92func addNote(to page: PDFPage, annotation: PDFAnnotation) {93 page.addAnnotation(annotation)94}95```9697### DON'T: Compare PDFDocument with == in UIViewRepresentable9899`PDFDocument` is a reference type. Use identity (`!==`).100101```swift102// WRONG: Always replaces document103func updateUIView(_ pdfView: PDFView, context: Context) {104 pdfView.document = document105}106107// CORRECT108func updateUIView(_ pdfView: PDFView, context: Context) {109 if pdfView.document !== document {110 pdfView.document = document111 }112}113```114115## Review Checklist116117- [ ] `PDFDocument` init uses optional binding, not force-unwrap118- [ ] `pdfView.autoScales = true` set for proper initial display119- [ ] Page indices checked against `pageCount` before access120- [ ] `displayMode` and `displayDirection` configured to match design121- [ ] Annotations use PDF coordinate space (origin bottom-left, Y up)122- [ ] All PDFKit mutations happen on the main thread123- [ ] Password-protected PDFs handled with `isLocked` / `unlock(withPassword:)`124- [ ] SwiftUI wrapper uses `!==` identity check in `updateUIView`125- [ ] `PDFViewPageChanged` notification observed for page tracking126- [ ] `PDFThumbnailView.pdfView` linked to the main `PDFView`127- [ ] Large-document search uses async `beginFindString` with delegate128- [ ] Saved documents use `write(to:withOptions:)` when encryption needed129130## References131132- PDF generation, forms, and printing: [references/pdf-generation-and-forms.md](references/pdf-generation-and-forms.md)133- PDF viewing, annotations, and overlays: [references/pdf-viewing-and-annotations.md](references/pdf-viewing-and-annotations.md)134- [PDFKit framework](https://sosumi.ai/documentation/pdfkit)135- [PDFView](https://sosumi.ai/documentation/pdfkit/pdfview)136- [PDFDocument](https://sosumi.ai/documentation/pdfkit/pdfdocument)137- [PDFPage](https://sosumi.ai/documentation/pdfkit/pdfpage), [PDFAnnotation](https://sosumi.ai/documentation/pdfkit/pdfannotation), [PDFSelection](https://sosumi.ai/documentation/pdfkit/pdfselection), [PDFThumbnailView](https://sosumi.ai/documentation/pdfkit/pdfthumbnailview)138- [PDFPageOverlayViewProvider](https://sosumi.ai/documentation/pdfkit/pdfpageoverlayviewprovider)139- [Adding Widgets to a PDF Document](https://sosumi.ai/documentation/pdfkit/adding-widgets-to-a-pdf-document)140- [Adding Custom Graphics to a PDF](https://sosumi.ai/documentation/pdfkit/adding-custom-graphics-to-a-pdf)141- [Core implementation details](references/core-implementation.md) -- setup, API wiring, and focused implementation recipes moved out of the entrypoint.