Skip to Content
NexPDF

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.

English

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)
KeyWhat it does on iOS
modeConfig.initialMode, availableModesThe mode to open in and the modes offered: viewer, annotations, content, forms, redaction. With one mode, the switcher is hidden
modeConfig.readOnlyViewing only: hides undo, redo, open, the page editor and every editing row
toolbarConfig.hiddenItemsBuilt-in items to hide, such as search, pages, more, close, or More menu rows as menu.<id>
toolbarConfig.customItemsYour own buttons: id, title, icon, location (top, toolbar or more) and modes
annotationsConfigavailableTools, defaultColor, defaultOpacity, colorPalette, presets, rememberStyles, stamps
formsConfig.availableToolsThe form field tools to offer
readerConfig.nightModeShows pages with inverted colours
globalConfigthemeMode (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, fileNameThe 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:

Eventdata
documentLoaded, documentChangedfilename, pageCount, canUndo, canRedo
modeChangedmode
pageChangedpage (1-based), pageCount
customItemTappedid, mode, page
closedfilename
NexPdfReaderView(document: pdf, configuration: config) { event in
    if event.kind == .customItemTapped, event.data["id"] as? String == "send" {
        upload()
    }
}

On this page