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

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 results are JSON strings. The examples parse them with Jackson; any JSON library works.

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; null keeps the current value.

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nexdevit.nexpdf.NexPdfDocument;
import java.nio.file.Path;

ObjectMapper json = new ObjectMapper();
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("report.pdf"))) {
    JsonNode info = json.readTree(doc.metadataJson());
    System.out.println(info.path("title").asText() + " / " + info.path("author").asText());

    doc.setMetadata("Q3 report", "Finance", null, null);
    doc.save(Path.of("report-titled.pdf"));
}

Bookmarks

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

static void walk(JsonNode items, int depth) {
    for (JsonNode item : items) {
        System.out.println("  ".repeat(depth) + item.get("title").asText()
                + " (page " + (item.get("page").asInt() + 1) + ")");
        walk(item.path("children"), depth + 1);
    }
}

walk(json.readTree(doc.outlinesJson()), 0);

doc.setOutlinesJson("""
        [{"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.

JsonNode labels = json.readTree(doc.pageLabelsJson());
System.out.println(labels.get("labels"));          // e.g. ["i","ii","1","2"]

doc.setPageLabelsJson("[{\"start\":0,\"style\":\"roman\"},{\"start\":2,\"style\":\"decimal\",\"first\":1}]");
doc.setPageLabelsJson("[]");                       // 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.

for (JsonNode layer : json.readTree(doc.layersJson())) {
    System.out.println(layer.get("name").asText() + (layer.get("visible").asBoolean() ? " visible" : " hidden"));
    if (layer.get("name").asText().equals("Notes")) {
        doc.setLayerVisible(layer.get("num").asInt(), (short) layer.get("gen").asInt(), 0);   // hide; 1 shows it again
    }
}
byte[] png = doc.renderPagePng(0, 144);             // rendered without the hidden 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 (JsonNode a : json.readTree(doc.attachmentsJson())) {
    System.out.println(a.get("name").asText() + " " + a.get("mime").asText() + " "
            + a.get("size").asLong() + " " + a.get("page"));
}

Files.write(Path.of("invoice.xml"), doc.attachmentData("invoice.xml"));

doc.addAttachment("data.csv", Files.readAllBytes(Path.of("data.csv")), "text/csv", "Source data");
doc.removeAttachment("old.csv");
doc.save(Path.of("with-data.pdf"));

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

On this page