PDFKit
Display, navigate, search, annotate, and manipulate PDF documents with PDFView, PDFDocument, PDFPage, PDFAnnotation, and PDFSelection.
Contents
- Setup
- Displaying PDFs
- Loading Documents
- Page Navigation
- Text Search and Selection
- Annotations
- Thumbnails
- SwiftUI Integration
- Common Mistakes
- Review Checklist
- References
Setup
PDFKit requires no entitlements or Info.plist entries.
Displaying PDFs
PDFView renders PDF content and handles zoom, scrolling, text selection, and page navigation.
Display Modes
Scaling and Appearance
Loading Documents
PDFDocument loads from a URL, Data, or can be created empty.
Password-Protected PDFs
Saving and Page Manipulation
Page Navigation
PDFView provides built-in navigation with history tracking.
Observing Page Changes
Text Search and Selection
Synchronous Search
Asynchronous Search
Use PDFDocumentDelegate for background searches on large documents.
Implement didMatchString(_:) to receive each match and
documentDidEndDocumentFind(_:) for completion.
Incremental Search and Find Interaction
Text Extraction
Highlighting Search Results
Annotations
Annotations are created with PDFAnnotation(bounds:forType:withProperties:)
and added to a PDFPage.
Highlight Annotation
Text Note Annotation
Free Text Annotation
Link Annotation
Removing Annotations
Common subtypes include .highlight, .underline, .strikeOut, .text,
.freeText, .ink, .link, .line, .square, .circle, .stamp, and
.widget.
Thumbnails
PDFThumbnailView
PDFThumbnailView shows a strip of page thumbnails linked to a PDFView.
Generating Thumbnails Programmatically
SwiftUI Integration
Wrap PDFView in a UIViewRepresentable for SwiftUI. PDF-specific wrappers
that configure PDFView, pages, annotations, search, thumbnails, or overlays
belong in this skill; route only generic representable lifecycle, layout, or SwiftUI state architecture questions to SwiftUI/UIKit interop guidance.
Usage
For interactive wrappers with page tracking, annotation hit detection, and coordinator patterns, see references/pdfkit-patterns.md [blocked].
Page Overlays
PDFPageOverlayViewProvider places UIKit views on top of individual pages
for interactive controls or custom rendering beyond standard annotations.
pageOverlayViewProvider is weak, so keep the provider strongly owned. For overlay lifecycle and save handling, read references/pdfkit-patterns.md [blocked].
Common Mistakes
DON'T: Force-unwrap PDFDocument init
PDFDocument(url:) and PDFDocument(data:) are failable initializers.
DON'T: Forget autoScales on PDFView
Without autoScales, the PDF renders at its native resolution.
DON'T: Ignore PDF coordinate system in annotations
PDF page coordinates have origin at the bottom-left with Y increasing upward -- opposite of UIKit.
DON'T: Modify annotations on a background thread
PDFKit classes are not thread-safe.
DON'T: Compare PDFDocument with == in UIViewRepresentable
PDFDocument is a reference type. Use identity (!==).
Review Checklist
-
PDFDocumentinit uses optional binding, not force-unwrap -
pdfView.autoScales = trueset for proper initial display - Page indices checked against
pageCountbefore access -
displayModeanddisplayDirectionconfigured to match design - Annotations use PDF coordinate space (origin bottom-left, Y up)
- All PDFKit mutations happen on the main thread
- Password-protected PDFs handled with
isLocked/unlock(withPassword:) - SwiftUI wrapper uses
!==identity check inupdateUIView -
PDFViewPageChangednotification observed for page tracking -
PDFThumbnailView.pdfViewlinked to the mainPDFView - Large-document search uses async
beginFindStringwith delegate - Saved documents use
write(to:withOptions:)when encryption needed
References
- Extended patterns (forms, watermarks, merging, printing, overlays, outlines, custom drawing): references/pdfkit-patterns.md [blocked]
- PDFKit framework
- PDFView
- PDFDocument
- PDFPage, PDFAnnotation, PDFSelection, PDFThumbnailView
- PDFPageOverlayViewProvider
- Adding Widgets to a PDF Document
- Adding Custom Graphics to a PDF


