Reader
Show the ready-made NexPDF reader in a SwiftUI or UIKit app, configure its modes, toolbar, tools and theme, and react to its events.
NexPdfUI
The reader is the NexPdfUI product of the Swift package (or the NexPdfUI pod), iOS only. Its
editing modes call the same engine functions as the API, so each needs the license module of that
feature; viewing works without a key.
The reader is a complete PDF screen: page view, search, page editor, bookmarks, attachments, and the modes viewer, annotations, content (text and image editing), forms and redaction.
Show the reader
import NexPdfUI
import SwiftUI
struct ContractView: View {
let pdf: Data
var body: some View {
NexPdfReaderView(document: pdf, fileName: "contract.pdf") { event in
print("NexPDF event:", event.type, event.data)
}
}
}Without a document, the reader opens empty with its Open button.
Configure it
NexPdfUIConfiguration is the shared NexPDF UI configuration, the same JSON as on the web,
Android, Flutter and React Native. Every key is optional and unknown keys are ignored.
let config = try NexPdfUIConfiguration(json: """
{
"modeConfig": { "initialMode": "annotations", "availableModes": ["viewer", "annotations", "forms"] },
"toolbarConfig": {
"hiddenItems": ["search"],
"customItems": [{ "id": "send", "title": "Send to CRM", "icon": "send", "location": "top" }]
},
"annotationsConfig": { "availableTools": ["ink", "highlight", "note"], "defaultColor": "#0F766E" },
"globalConfig": { "themeMode": "dark", "accentColor": "#0F766E", "language": "pt" }
}
""")
NexPdfReaderView(document: pdf, configuration: config)| Key | What it does on iOS |
|---|---|
modeConfig.initialMode, availableModes | The mode to open in and the modes offered: viewer, annotations, content, forms, redaction. With one mode, the switcher is hidden |
modeConfig.readOnly | Viewing only: hides undo, redo, open, the page editor and every editing row |
toolbarConfig.hiddenItems | Built-in items to hide, such as search, pages, more, close, or More menu rows as menu.<id> |
toolbarConfig.customItems | Your own buttons: id, title, icon, location (top, toolbar or more) and modes |
annotationsConfig | availableTools, defaultColor, defaultOpacity, colorPalette, presets, rememberStyles, stamps |
formsConfig.availableTools | The form field tools to offer |
readerConfig.nightMode | Shows pages with inverted colours |
globalConfig | themeMode (system, light, dark), accentColor, language, strings, languages |
Layout, zoom, item order, defaultFontSize, brand and CSS keys are web only and are ignored on iOS.
Drive the reader from the app
Pass a NexPdfReaderController to open documents, switch modes and read back the edited file:
@StateObject private var reader = NexPdfReaderController()
var body: some View {
NexPdfReaderView(configuration: config, controller: reader)
.toolbar {
Button("Save") {
if let data = reader.documentData() {
try? data.write(to: outputURL)
}
}
}
}
func load(_ data: Data) {
do {
try reader.open(data: data, name: "contract.pdf")
reader.goToPage(3) // 1-based
reader.setMode("annotations")
} catch NexPdfReaderController.OpenError.passwordRequired {
// ask for the password, then call open(data:name:password:)
} catch {
print(error)
}
}| Member | |
|---|---|
open(data:name:password:) | Opens a PDF (or a Word, Excel or PowerPoint file, converted). Throws OpenError.passwordRequired, .failed(message) or .notReady (the reader is not on screen yet) |
documentData() | The current PDF with every change applied |
pageCount, currentPage, mode, fileName | The reader state. currentPage is 1-based |
setMode(_:), goToPage(_:) | Switch mode (ignored if the mode is not available) and go to a 1-based page |
undo(), redo() | Undo and redo (ignored in read-only mode) |
NexPdfReaderViewController creates its own controller, available as its controller property.
Events
The onEvent closure receives a NexPdfUIEvent with kind, type (the kind as a string) and
data:
| Event | data |
|---|---|
documentLoaded, documentChanged | filename, pageCount, canUndo, canRedo |
modeChanged | mode |
pageChanged | page (1-based), pageCount |
customItemTapped | id, mode, page |
closed | filename |
NexPdfReaderView(document: pdf, configuration: config) { event in
if event.kind == .customItemTapped, event.data["id"] as? String == "send" {
upload()
}
}