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 Rust.

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).

The examples use the serde feature for the *_json_value functions; without it, each *_json function returns the same JSON as a String.

Metadata

metadata_json() returns the document information dictionary (title, author, subject, keywords and the other entries the file has). set_metadata(title, author, subject, keywords) changes it; None keeps the current value.

use nexpdf_sdk::Document;

fn main() -> nexpdf_sdk::Result<()> {
    let mut doc = Document::open("report.pdf")?;
    let info = doc.metadata_json_value()?;
    println!("{} {}", info["title"], info["author"]);

    doc.set_metadata("Q3 report", "Finance", None, None)?;
    doc.save("report-titled.pdf")
}

Bookmarks

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

fn walk(items: &serde_json::Value, depth: usize) {
    for item in items.as_array().into_iter().flatten() {
        let page = item["page"].as_u64().unwrap_or(0) + 1;
        println!("{}{} (page {page})", "  ".repeat(depth), item["title"].as_str().unwrap_or(""));
        walk(&item["children"], depth + 1);
    }
}

walk(&doc.outlines_json_value()?, 0);

doc.set_outlines_json(
    r#"[{"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. page_labels_json() returns the label of every page and the ranges that produce them.

let labels = doc.page_labels_json_value()?;
println!("{}", labels["labels"]);           // e.g. ["i","ii","1","2",...]

doc.set_page_labels_json(r#"[{"start":0,"style":"roman"},{"start":2,"style":"decimal","first":1}]"#)?;
doc.set_page_labels_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 = doc.layers_json_value()?;
for layer in layers.as_array().into_iter().flatten() {
    let state = if layer["visible"].as_bool() == Some(true) { "visible" } else { "hidden" };
    println!("{} {state}", layer["name"]);
}

if let Some(notes) = layers.as_array().into_iter().flatten().find(|l| l["name"] == "Notes") {
    let num = notes["num"].as_u64().unwrap_or(0) as u32;
    let generation = notes["gen"].as_u64().unwrap_or(0) as u16;
    doc.set_layer_visible(num, generation, 0)?;        // hide; 1 shows it again
}
let png = doc.render_page_png(0, 144.0)?;              // rendered without the layer

Attachments

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

for a in doc.attachments_json_value()?.as_array().into_iter().flatten() {
    println!("{} {} {} {}", a["name"], a["mime"], a["size"], a["page"]);
}

std::fs::write("invoice.xml", doc.attachment_data("invoice.xml")?)?;

doc.add_attachment("data.csv", &std::fs::read("data.csv")?, "text/csv", "Source data")?;
doc.remove_attachment("old.csv")?;
doc.save("with-data.pdf")?;

To attach a file to a page with a visible icon, use add_file_attachment_annot; see Annotations.

On this page