Skip to Content
NexPDF

Opening and saving

Open PDFs from files and Data, open encrypted files, create PDFs from pictures, HTML, Office and e-book files, and save, linearize and copy documents with NexPDF for iOS (Swift).

English

License module

Opening PDFs and pictures, saving and copying are part of the viewer module and work without a key. Creating a PDF from HTML, Office or e-book files needs the conversion module (Startup tier and above).

Open a file or Data

NexPdfDocument.open(url:password:) reads a file; open(data:password:) takes the bytes you already have, for example from the share sheet, a document picker or a download.

import NexPdf

let doc = try NexPdfDocument.open(url: contractURL)
print(doc.pages, "pages")

let data = try await URLSession.shared.data(from: remoteURL).0
let downloaded = try NexPdfDocument.open(data: data)

Both accept JPEG, PNG and TIFF pictures too (one page per picture). With the conversion module, Word, Excel and PowerPoint files, EPUB and zipped FictionBook books, XPS files and CBZ comics are detected and converted on open.

A file from a document picker is security-scoped. Read it while you hold access:

func open(picked url: URL) throws -> NexPdfDocument {
    let access = url.startAccessingSecurityScopedResource()
    defer { if access { url.stopAccessingSecurityScopedResource() } }
    return try NexPdfDocument.open(url: url)    // the bytes are read now; access can end afterwards
}

A document holds native memory and is freed when the last reference to it goes away. There is no close(); set the variable to nil or let it go out of scope.

Encrypted documents

Pass the user (open) password or the owner password. RC4, AES-128 and AES-256 files are supported.

do {
    let doc = try NexPdfDocument.open(url: lockedURL, password: "s3cret")
} catch let error as NexPdfError where error.message == "password required" || error.message == "incorrect password" {
    askTheUserAgain()
}

Files that are only restricted by permissions (no open password) open without a password. securityJson() reports the encryption and permissions; see Security.

Create a PDF from other formats

SourceFunctionModule
JPEG, PNG, TIFFNexPdfDocument.openImage(data:)none
HTMLNexPdfDocument.fromHtml(html:)conversion
Word, Excel, PowerPointNexPdfDocument.openOffice(data:)conversion
EPUB, FictionBook, XPS/OpenXPS, CBZNexPdfDocument.openEbook(data:)conversion
let scan = try NexPdfDocument.openImage(data: try Data(contentsOf: tiffURL))   // one page per TIFF frame
try scan.write(to: scanURL)

let invoice = try NexPdfDocument.fromHtml(html: "<h1>Invoice 42</h1><p>Total: 120 EUR</p>")
try invoice.write(to: invoiceURL)

if let photo = image.jpegData(compressionQuality: 0.9) {
    let pdf = try NexPdfDocument.openImage(data: photo)                        // a camera photo
}

To add pictures to an existing document, use insertImagePages(index:data:); see Editing.

Save

saveBytes() returns the document with every change as a complete new file. write(to:) writes the same bytes to a file atomically.

try doc.write(to: outURL)
let pdfData = try doc.saveBytes()          // e.g. to upload or to share

Fast Web View

A linearized file shows its first page before the download ends, which helps when PDFs are served over HTTP:

let webData = try doc.saveLinearized()
try webData.write(to: webURL)

Signed files

Signing returns the finished signed file. Write those bytes as they are: saving the document object again rewrites the file and breaks the signature. See Signatures.

Copy a document

copy() returns an independent document with the same content and changes. Use it as an undo snapshot, or to hand work to another queue:

var current = try NexPdfDocument.open(url: url)
let snapshot = try current.copy()
try current.deletePage(pageIndex: 0)
// ... undo:
current = snapshot

copy() saves the document and opens the saved bytes, so it costs about as much as a save.

In the reader

The reader opens files with NexPdfReaderController.open(data:name:password:) and gives back the edited PDF with documentData().

On this page