Skip to Content
NexPDF

Document structure

Read and set metadata, bookmarks and page labels, show and hide layers, and embed or extract file attachments with NexPDF for iOS (Swift).

English

License module

Reading metadata, bookmarks, page labels, layers and attachments, and switching layers on or off, work without a key. Changing metadata, bookmarks, page labels and attachments needs the editor module (Startup tier and above).

Methods whose name ends in Json return JSON text; NexPdf.json(_:) parses it into Foundation objects.

Metadata

metadataJson() returns the document information dictionary (title, author, subject, keywords and the other entries the file has). setMetadata(title:author:subject:keywords:) changes it; nil keeps the current value.

import NexPdf

let doc = try NexPdfDocument.open(url: reportURL)
let info = NexPdf.json(try doc.metadataJson()) as? [String: Any] ?? [:]
print(info["title"] ?? "", info["author"] ?? "")

try doc.setMetadata(title: "Q3 report", author: "Finance")
try doc.write(to: titledURL)

Bookmarks

Bookmarks (the outline) are a JSON tree of title, 0-based page and optional children. setOutlinesJson(json:) replaces the whole tree.

func walk(_ items: [[String: Any]], depth: Int = 0) {
    for item in items {
        let page = (item["page"] as? Int ?? 0) + 1
        print(String(repeating: "  ", count: depth) + "\(item["title"] ?? "") (page \(page))")
        walk(item["children"] as? [[String: Any]] ?? [], depth: depth + 1)
    }
}
walk(NexPdf.json(try doc.outlinesJson()) as? [[String: Any]] ?? [])

try doc.setOutlinesJson(json: #"[{"title":"Summary","page":0},{"title":"Details","page":2,"children":[{"title":"Costs","page":3}]}]"#)

Page labels

Page labels are the page numbers a viewer shows, such as i, ii for a preface and 1, 2 after it. pageLabelsJson() returns the label of every page and the ranges that produce them.

let labels = NexPdf.json(try doc.pageLabelsJson()) as? [String: Any]
print((labels?["labels"] as? [String] ?? []).prefix(4))     // e.g. ["i", "ii", "1", "2"]

try doc.setPageLabelsJson(json: #"[{"start":0,"style":"roman"},{"start":2,"style":"decimal","first":1}]"#)
try doc.setPageLabelsJson(json: "[]")                       // removes the labels

Styles are decimal, roman, ROMAN, letters, LETTERS and none. A range can also have a prefix, for example "A-".

Layers

Layers (optional content groups) are parts of a page that can be shown or hidden, common in CAD drawings and maps. Each layer is identified by its object number and generation.

let layers = NexPdf.json(try doc.layersJson()) as? [[String: Any]] ?? []
for layer in layers {
    print(layer["name"] ?? "", (layer["visible"] as? Bool ?? false) ? "visible" : "hidden")
}

if let notes = layers.first(where: { $0["name"] as? String == "Notes" }),
   let num = notes["num"] as? Int, let gen = notes["gen"] as? Int {
    try doc.setLayerVisible(num: UInt32(num), generation: UInt16(gen), visible: 0)   // hide; 1 shows it again
    let png = try doc.renderPagePng(pageIndex: 0, dpi: 144)                          // rendered without the layer
}

Attachments

PDFs can carry files: at document level (an attachment list) or on a page (a paperclip annotation). attachmentsJson() lists both; page is null for document-level files.

for a in NexPdf.json(try doc.attachmentsJson()) as? [[String: Any]] ?? [] {
    print(a["name"] ?? "", a["mime"] ?? "", a["size"] ?? 0, a["page"] ?? "document")
}

let xml = try doc.attachmentData(name: "invoice.xml")
try xml.write(to: xmlURL)

try doc.addAttachment(name: "data.csv", data: csvData, mime: "text/csv", description: "Source data")
try doc.removeAttachment(name: "old.csv")
try doc.write(to: withDataURL)

To attach a file to a page with a visible icon, use addFileAttachmentAnnot; see Annotations. The reader shows bookmarks and attachments to the user.

On this page