PDFKit (iOS)
Render, navigate, annotate, search, and fill PDFs. import PDFKit. iOS 11+;
this skill targets the iOS 26 era. PDFKit is UIKit-based — there is no native
SwiftUI PDFView, so wrap it with UIViewRepresentable (see swiftui-uikit-interop).
- Threading:
PDFViewand all UI live on the main thread. Loading a largePDFDocument, rendering thumbnails, andwrite(to:)are expensive — run them off the main actor and hand the finished object back to the view. - Fintech note: never write account statements/receipts to a world-readable path.
Export to an app-sandbox URL and, if it leaves the device or persists, encrypt at
rest — see
swift-security. PDFKit's own owner/user passwords (below) are AcroForm encryption, not a substitute for file-level protection.
Contents
- Display in SwiftUI
- PDFDocument — load, save, encrypt
- PDFPage — bounds, thumbnail, render
- Navigate, select, search
- Annotations
- Forms (AcroForm widgets)
- Thumbnails & outline
- PDFDocumentDelegate
- Correctness checklist
- References:
references/annotations-forms.md,references/display-search.md
<a id="display"></a>
Display in SwiftUI
PDFView is a UIView. Bridge it once and drive it through the document.
import SwiftUI
import PDFKit
struct PDFKitView: UIViewRepresentable {
let document: PDFDocument
func makeUIView(context: Context) -> PDFView {
let view = PDFView()
view.autoScales = true // fit-to-width, tracks rotation/resize
view.displayMode = .singlePageContinuous // .singlePage | .twoUp | .twoUpContinuous
view.displayDirection = .vertical // or .horizontal
view.usePageViewController(false) // true = page-curl (iBooks-style)
return view
}
func updateUIView(_ view: PDFView, context: Context) {
if view.document !== document { view.document = document }
}
}
- Load off-main, then assign on main:
func loadDocument(at url: URL) async -> PDFDocument? {
await Task.detached(priority: .userInitiated) { PDFDocument(url: url) }.value
}
- Key
PDFViewknobs:autoScales,scaleFactor/minScaleFactor/maxScaleFactor,scaleFactorForSizeToFit,pageBreakMargins,backgroundColor,pageShadowsEnabled. PDFView.documentis optional; setting it re-lays-out.currentPage,go(to:),goToNextPage(_:)/goToPreviousPage(_:)for programmatic navigation.- Observe
.PDFViewPageChanged,.PDFViewScaleChanged,.PDFViewSelectionChanged(post fromNotificationCenter) to reflect state in SwiftUI.
<a id="document"></a>
PDFDocument — load, save, encrypt
let doc = PDFDocument(url: fileURL) // returns nil if unreadable/corrupt
let doc2 = PDFDocument(data: pdfData) // from a network/blob payload
let count = doc?.pageCount ?? 0
let page = doc?.page(at: 0) // 0-based; nil out of range
let index = page.map { doc?.index(for: $0) }
doc?.insert(newPage, at: count) // also removePage(at:), exchangePage(at:withPageAt:)
- Save:
write(to:)/write(to:withOptions:)returnBool;dataRepresentation()returnsData. Both flatten in-memory edits (added pages, annotations) into the output.
let ok = doc.write(to: outURL) // Bool — check it
let data = doc.dataRepresentation() // for upload / Keychain-adjacent storage
- Encryption / permissions — supply passwords as write options (AcroForm-level):
let opts: [PDFDocumentWriteOption: Any] = [
.userPasswordOption: userPassword, // required to open
.ownerPasswordOption: ownerPassword, // required to change permissions
]
doc.write(to: outURL, withOptions: opts)
- Reading a locked doc:
isEncrypted/isLockedaretrueuntil you callunlock(withPassword:)(returnsBool).allowsCopying,allowsPrinting,allowsCommenting,allowsFormFieldEntryreport the permission flags. - Metadata:
documentAttributes([AnyHashable: Any]) keyed byPDFDocumentAttribute(.titleAttribute,.authorAttribute,.creationDateAttribute, …). - Do not hardcode passwords; source them from the user or Keychain (
swift-security).
<a id="page"></a>
PDFPage — bounds, thumbnail, render
A PDFPage exposes geometry, a bitmap, and raw drawing. PDFDisplayBox selects the
box: .mediaBox (full sheet), .cropBox (visible, PDFKit's default), .bleedBox,
.trimBox, .artBox.
let bounds = page.bounds(for: .cropBox) // CGRect in PDF (Core Graphics) space
let rotation = page.rotation // 0/90/180/270
let text = page.string // full page text, or nil
let n = page.numberOfCharacters
- Thumbnail (returns a
UIImage, cheap for list cells):
let thumb = page.thumbnail(of: CGSize(width: 120, height: 160), for: .cropBox)
- Custom render into your own context (export, share sheet, watermark):
let box = PDFDisplayBox.cropBox
let pageRect = page.bounds(for: box)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
let image = renderer.image { ctx in
UIColor.white.set(); ctx.fill(pageRect)
let cg = ctx.cgContext
cg.translateBy(x: 0, y: pageRect.height) // flip: PDF origin is bottom-left
cg.scaleBy(x: 1, y: -1)
page.draw(with: box, to: cg) // renders page content + annotations
}
- PDF coordinate space is bottom-left origin; UIKit is top-left. Flip the CTM (as
above) whenever you draw a page into a UIKit/
CGContexttarget, and when converting view points usepage.convert(_:to:)/PDFView.convert(_:to:).
<a id="navsearch"></a>
Navigate, select, search
Text extraction via PDFSelection:
let whole = page.string // per page
let range = page.selection(for: NSRange(location: 0, length: 20)) // chars → selection
let sel = doc.selection(from: startPos, to: endPos) // across pages (PDFDestination)
let picked = sel?.string // extracted text
Search — synchronous returns [PDFSelection]; options are NSString.CompareOptions
(.caseInsensitive, .backwards, .literal). PDFKit does not honor
.regularExpression.
let hits = doc.findString("overdue", withOptions: [.caseInsensitive])
if let first = hits.first {
first.color = .yellow
pdfView.setCurrentSelection(first, animate: true)
pdfView.go(to: first) // scroll into view
}
pdfView.highlightedSelections = hits // persistent highlight overlay
- Incremental search:
findString(_:fromSelection:withOptions:)(one next hit), or the asyncbeginFindString(_:withOptions:)which posts.PDFDocumentDidBeginFind/.PDFDocumentDidFindMatch/.PDFDocumentDidEndFind— use it for large docs so the UI never blocks. PDFSelection:.pages,.string,.attributedString,.bounds(for:),.selectionsByLine()(split multi-line selections into per-line rects — needed to lay down accurate highlight annotations). Combine with.add(_:)/extend(...).
<a id="annotations"></a>
Annotations
Create with the modern initializer init(bounds:forType:withProperties:) — bounds is
in page coordinates, forType is a PDFAnnotationSubtype. Then page.addAnnotation(_:).
// Free-text note
let note = PDFAnnotation(bounds: CGRect(x: 40, y: 700, width: 200, height: 24),
forType: .freeText, withProperties: nil)
note.contents = "Reviewed 2026-08-03"
note.font = .systemFont(ofSize: 12)
note.fontColor = .label
note.color = .clear // background fill
page.addAnnotation(note)
// Highlight the current search/text selection (one annotation per visual line)
for lineSel in selection.selectionsByLine() {
guard let p = lineSel.pages.first else { continue }
let h = PDFAnnotation(bounds: lineSel.bounds(for: p),
forType: .highlight, withProperties: nil)
h.color = .systemYellow // .markupType is .highlight by subtype
p.addAnnotation(h)
}
// Ink (freehand) — attach one or more UIBezierPaths in page space
let ink = PDFAnnotation(bounds: page.bounds(for: .cropBox),
forType: .ink, withProperties: nil)
ink.color = .systemBlue
ink.add(bezierPath) // repeat for multiple strokes; .paths reads them back
page.addAnnotation(ink)
- Subtypes:
.highlight,.underline,.strikeOut,.freeText,.ink,.square,.circle,.line,.stamp,.text(note icon),.link,.widget(forms, below). - Read
annotation.type(raw subtype string) and.bounds; enumeratepage.annotations. - Remove with
page.removeAnnotation(_:). Edits are in-memory until youwrite(to:)/dataRepresentation(). - Deep dive (markup colors,
PDFBorder, stamps, links,PDFAnnotationKeyproperties) inreferences/annotations-forms.md.
<a id="forms"></a>
Forms (AcroForm widgets)
Form fields are .widget annotations. Locate them by fieldName, set the value, and
save. Never guess field names — enumerate them first.
func setField(_ name: String, to value: String, in doc: PDFDocument) {
for i in 0..<doc.pageCount {
guard let page = doc.page(at: i) else { continue }
for a in page.annotations where a.fieldName == name {
switch a.widgetFieldType {
case .text:
a.widgetStringValue = value
case .button:
// checkbox / radio: match the field's on-state string
a.buttonWidgetState = (value == a.buttonWidgetStateString) ? .onState : .offState
case .choice:
a.widgetStringValue = value // list/combo selection
default:
break
}
}
}
}
- Field metadata:
widgetFieldType(.text,.button,.choice,.signature),widgetControlType(.pushButtonControl,.radioButtonControl,.checkBoxControl),fieldName,isReadOnly,maximumLength,hasComb; choice options via the.widgetOptionsannotation key. - Text value ↔
widgetStringValue; checkbox/radio state ↔buttonWidgetState(.onState/.offState), whose "on" name isbuttonWidgetStateString. - Flatten before sharing if the recipient shouldn't edit fields: render each page with
page.draw(with:to:)into a freshUIGraphicsPDFRenderercontext (widgets become static pixels). Full flattening recipe inreferences/annotations-forms.md. - Respect
allowsFormFieldEntry; a locked/encrypted doc may reject edits until unlocked.
<a id="thumbnails-outline"></a>
Thumbnails & outline
PDFThumbnailView — a ready-made page navigator; bind it to the same PDFView.
let thumbs = PDFThumbnailView()
thumbs.pdfView = pdfView // shares selection/navigation with the main view
thumbs.thumbnailSize = CGSize(width: 80, height: 100)
thumbs.layoutMode = .vertical // or .horizontal
PDFOutline — the document's table of contents (bookmarks):
if let root = doc.outlineRoot {
for i in 0..<root.numberOfChildren {
let item = root.child(at: i)!
let title = item.label // section title
if let dest = item.destination { // PDFDestination: page + point
// dest.page, dest.point — go(to:) it on tap
}
}
}
- Outline is a tree — recurse via
numberOfChildren/child(at:).doc.outlineItem(for:)maps a selection back to its outline node.
<a id="delegate"></a>
PDFDocumentDelegate
Set doc.delegate to customize page behavior or observe unlock/find events.
classForPage()— return aPDFPagesubclass so every page uses your customdraw(with:to:)(e.g. stamp a "COPY" / account-number watermark on render without mutating the file):
final class WatermarkPage: PDFPage {
override func draw(with box: PDFDisplayBox, to context: CGContext) {
super.draw(with: box, to: context) // draw the real page first
context.saveGState()
// …draw watermark text into `context` (page space, bottom-left origin)…
context.restoreGState()
}
}
final class DocDelegate: NSObject, PDFDocumentDelegate {
func classForPage() -> AnyClass { WatermarkPage.self }
func documentDidUnlock(_ notification: Notification) { /* re-enable UI */ }
}
- Also:
classForAnnotationType(_:)to substitute a custom annotation class, and the find-delegate callbacks (didMatchString(_:)) for async search results.
<a id="checklist"></a>
Correctness checklist
-
PDFViewcreated/mutated only on the main thread;PDFDocument(url:), thumbnails, andwrite(to:)run off-main, result handed back on main. - SwiftUI integration goes through
UIViewRepresentable(no native SwiftUI PDFView);updateUIViewswapsdocumentonly when it actually changed (!==). - Return values checked:
PDFDocument(url:)andpage(at:)are optional;write(to:)andunlock(withPassword:)returnBool. - Page drawing into a
CGContextflips the CTM (PDF is bottom-left origin); point conversions useconvert(_:to:), not manual math. - Correct
PDFDisplayBoxchosen (.cropBoxfor what the user sees,.mediaBoxfor the full sheet) consistently across bounds/thumbnail/draw. - Annotation
boundsare in page coordinates; highlights useselectionsByLine()rects; annotations added withaddAnnotationand persisted viawrite(to:)/dataRepresentation(). - Form fields located by
fieldName(enumerated, not guessed); text→widgetStringValue, button→buttonWidgetStatevsbuttonWidgetStateString; flatten before sharing read-only. - Search treats
findStringas synchronous/blocking on large docs — usebeginFindString+ notifications instead;.regularExpressionis unsupported. - Exported financial PDFs written to the app sandbox and protected at rest
(
swift-security); passwords never hardcoded.