Getting Started with PDF Oxide (Swift)
PDF Oxide is the fastest PDF library with built-in text extraction — 0.8ms mean, 100% pass rate on 3,830 PDFs. The Swift binding, new in v0.3.69, wraps the Rust core through a C ABI: handles are owned by classes (freed in deinit), C buffers are copied into Swift String/[UInt8], and error codes are thrown as PdfOxideError.
Installation
The binding links the default-feature cdylib. Build the native library, then point SwiftPM at the headers and library:
# 1. build the native library (shipped binding feature set)
cargo build --release --lib --features ocr,rendering,signatures,barcodes,tsa-client,system-fonts
# 2. test + run the example (Package.swift reads PDF_OXIDE_INCLUDE_DIR / _LIB_DIR)
cd swift
export PDF_OXIDE_INCLUDE_DIR="$PWD/../include"
export PDF_OXIDE_LIB_DIR="$PWD/../target/release"
DYLD_LIBRARY_PATH="$PDF_OXIDE_LIB_DIR" swift test
DYLD_LIBRARY_PATH="$PDF_OXIDE_LIB_DIR" swift run basic_extraction
Quick Start
Build a PDF from Markdown, open it from the produced bytes, and extract its text. The whole round-trip runs without any external fixture:
import PdfOxide
let pdf = try Pdf.fromMarkdown("# Hello pdf_oxide\n\nThis is a **Swift** binding.\n")
let doc = try Document.openFromBytes(try pdf.toBytes())
print("pages: \(try doc.pageCount())")
print("version: \(try doc.version())")
print(try doc.extractText(0))
To open a file from disk, use Document.open(_:):
import PdfOxide
let doc = try Document.open("research-paper.pdf")
print("Pages: \(try doc.pageCount())")
print("Version: \(try doc.version())") // e.g. 1.7
Text Extraction
extractText(_:) returns the text of a single zero-based page. Loop over pageCount() to read the whole document:
import PdfOxide
let doc = try Document.open("book.pdf")
for i in 0..<(try doc.pageCount()) {
print("--- Page \(i + 1) ---")
print(try doc.extractText(i))
}
toPlainText(_:) gives a flattened, layout-free variant, and the *All() methods extract every page at once:
let doc = try Document.open("report.pdf")
let plain = try doc.toPlainText(0) // single page, no layout
let everything = try doc.toPlainTextAll() // all pages concatenated
Words & Characters
extractWords(_:) returns [Word] with a bounding box and font metadata for each word. extractChars(_:) returns [Char] with per-character positioning:
import PdfOxide
let doc = try Document.open("paper.pdf")
let words = try doc.extractWords(0)
for word in words.prefix(10) {
print("'\(word.text)' at (\(word.bbox.x), \(word.bbox.y)) "
+ "font=\(word.fontName) size=\(word.fontSize) bold=\(word.bold)")
}
let chars = try doc.extractChars(0)
for ch in chars.prefix(10) {
let scalar = Unicode.Scalar(ch.character).map(String.init) ?? "?"
print("'\(scalar)' size=\(ch.fontSize) font=\(ch.fontName)")
}
Word fields: text (String), bbox (Bbox), fontName (String), fontSize (Double), bold (Bool). Char fields: character (UInt32 codepoint), bbox, fontName, fontSize. A Bbox exposes x, y, width, and height as Double.
You can also pull text line by line with extractTextLines(_:), which returns [TextLine] (text, bbox, wordCount):
let lines = try doc.extractTextLines(0)
for line in lines {
print("\(line.wordCount) words: \(line.text)")
}
Markdown & HTML Conversion
Convert a single page or the entire document to Markdown or HTML:
import PdfOxide
let doc = try Document.open("paper.pdf")
let md = try doc.toMarkdown(0) // one page to Markdown
let mdAll = try doc.toMarkdownAll() // whole document to Markdown
let html = try doc.toHtml(0) // one page to HTML
let htmlAll = try doc.toHtmlAll() // whole document to HTML
print(mdAll)
Search
search(_:_:_:) searches a single page; searchAll(_:_:) searches the whole document. Both take a search term and a caseSensitive flag, returning [SearchResult] (text, page, bbox):
import PdfOxide
let doc = try Document.open("manual.pdf")
// Search a single page (page 0, case-insensitive)
let hits = try doc.search(0, "configuration", false)
for hit in hits {
print("page \(hit.page): '\(hit.text)' at (\(hit.bbox.x), \(hit.bbox.y))")
}
// Search the whole document
let allHits = try doc.searchAll("configuration", false)
print("\(allHits.count) total matches")
PDF Creation
The Pdf type provides factory methods that build a document from a source format. Save it to disk with save(_:) or grab the raw bytes with toBytes():
import PdfOxide
try Pdf.fromMarkdown("# Hello World\n\nThis is a PDF.").save("output.pdf")
try Pdf.fromHtml("<h1>Invoice</h1><p>Amount: $42</p>").save("invoice.pdf")
try Pdf.fromText("Plain text content.").save("notes.pdf")
let bytes = try Pdf.fromMarkdown("# In-memory\n\nbody\n").toBytes()
print("produced \(bytes.count) bytes")
Error Handling
Every fallible call throws PdfOxideError, which carries the failing operation name and the underlying C-ABI error code:
import PdfOxide
do {
let doc = try Document.open("document.pdf")
print(try doc.extractText(0))
} catch let error as PdfOxideError {
print("PDF error: \(error)") // e.g. "PdfOxideError: open failed (error code 1)"
}
Next Steps
- Rust Getting Started – using PDF Oxide from Rust
- Python Getting Started – using PDF Oxide from Python
- Text Extraction – detailed extraction options and recipes
- PDF Creation – advanced creation with metadata and encryption
- Editing – modifying existing PDFs, annotations, and form fields