Document structure
Read and set metadata, bookmarks and page labels, show and hide layers, and embed or extract file attachments with NexPDF for Android.
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).
Every call below is a static method of NexPdfJni that takes doc.handle(). JSON results are
parsed with org.json.
Metadata
metadataJson(handle) returns the document information dictionary (title, author, subject,
keywords and the other entries the file has). setMetadata(handle, title, author, subject, keywords) changes it; null keeps the current value.
import com.nexdevit.nexpdf.NexPdfDocument
import com.nexdevit.nexpdf.NexPdfJni
import org.json.JSONArray
import org.json.JSONObject
NexPdfDocument.openBytes(bytes).use { doc ->
val h = doc.handle()
val info = JSONObject(NexPdfJni.metadataJson(h))
Log.i("NexPDF", "${info.optString("title")} ${info.optString("author")}")
NexPdfJni.setMetadata(h, "Q3 report", "Finance", null, null)
val titled = doc.saveBytes()
}Bookmarks
Bookmarks (the outline) are a JSON tree of title, 0-based page and optional children.
setOutlinesJson replaces the whole tree.
fun walk(items: JSONArray, depth: Int = 0) {
for (i in 0 until items.length()) {
val item = items.getJSONObject(i)
Log.i("NexPDF", " ".repeat(depth) + "${item.getString("title")} (page ${item.getInt("page") + 1})")
item.optJSONArray("children")?.let { walk(it, depth + 1) }
}
}
walk(JSONArray(NexPdfJni.outlinesJson(h)))
NexPdfJni.setOutlinesJson(h, """[{"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, and
setPageLabelsJson(rangesJson) sets the ranges (NexPdfJni.pageLabelsJson(handle) and
setPageLabelsJson(handle, rangesJson) for a raw handle):
val labels = JSONObject(doc.pageLabelsJson())
Log.i("NexPDF", labels.getJSONArray("labels").toString()) // e.g. ["i","ii","1","2"]
check(doc.setPageLabelsJson("""[{"start":0,"style":"roman"},{"start":2,"style":"decimal","first":1}]""")) {
NexPdfJni.lastError()
}
val labelled = doc.saveBytes()Styles are decimal, roman, ROMAN, letters, LETTERS and none. A range can also have a
prefix, for example "A-". [] removes the labels.
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.
val layers = JSONArray(NexPdfJni.layersJson(h))
for (i in 0 until layers.length()) {
val layer = layers.getJSONObject(i)
Log.i("NexPDF", "${layer.getString("name")} ${if (layer.getBoolean("visible")) "visible" else "hidden"}")
}
val notes = (0 until layers.length()).map { layers.getJSONObject(it) }.first { it.getString("name") == "Notes" }
NexPdfJni.setLayerVisible(h, notes.getInt("num"), notes.getInt("gen"), false) // hide; true shows it again
val png = NexPdfJni.renderPagePng(h, 0, 144.0) // rendered without the layerAttachments
PDFs can carry files: at document level (an attachment list) or on a page (a paperclip annotation).
attachmentsJson(handle) lists both; page is null for document-level files.
val list = JSONArray(NexPdfJni.attachmentsJson(h))
for (i in 0 until list.length()) {
val a = list.getJSONObject(i)
Log.i("NexPDF", "${a.getString("name")} ${a.optString("mime")} ${a.optLong("size")} ${a.opt("page")}")
}
File(filesDir, "invoice.xml").writeBytes(NexPdfJni.attachmentData(h, "invoice.xml"))
NexPdfJni.addAttachment(h, "data.csv", csvBytes, "text/csv", "Source data")
NexPdfJni.removeAttachment(h, "old.csv")
val withData = doc.saveBytes()To attach a file to a page with a visible icon, use addFileAttachmentAnnot; see
Annotations. The
reader shows bookmarks and attachments to the user.
Related
- Editing: pages, text and images
- Opening and saving