Document structure
Read and set metadata, bookmarks and page labels, show and hide layers, and embed or extract file attachments with NexPDF for .NET.
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 System.Text.Json, which ships with .NET.
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.
using System.Text.Json.Nodes;
using Nexdevit.NexPdf;
using var doc = NexPdfDocument.Open("report.pdf");
var info = JsonNode.Parse(doc.MetadataJson())!;
Console.WriteLine($"{info["title"]} / {info["author"]}");
doc.SetMetadata(title: "Q3 report", author: "Finance");
doc.Save("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(JsonArray items, int depth = 0)
{
foreach (var item in items)
{
Console.WriteLine($"{new string(' ', depth * 2)}{item!["title"]} (page {item["page"]!.GetValue<int>() + 1})");
if (item["children"] is JsonArray children) Walk(children, depth + 1);
}
}
Walk(JsonNode.Parse(doc.OutlinesJson())!.AsArray());
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.
var labels = JsonNode.Parse(doc.PageLabelsJson())!;
Console.WriteLine(labels["labels"]!.ToJsonString()); // e.g. ["i","ii","1","2"]
doc.SetPageLabelsJson("""[{"start":0,"style":"roman"},{"start":2,"style":"decimal","first":1}]""");
doc.SetPageLabelsJson("[]"); // removes the labelsStyles 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.
var layers = JsonNode.Parse(doc.LayersJson())!.AsArray();
foreach (var layer in layers)
Console.WriteLine($"{layer!["name"]} {(layer["visible"]!.GetValue<bool>() ? "visible" : "hidden")}");
var notes = layers.First(l => l!["name"]!.GetValue<string>() == "Notes")!;
doc.SetLayerVisible(notes["num"]!.GetValue<uint>(), notes["gen"]!.GetValue<ushort>(), 0); // hide; 1 shows it again
var png = doc.RenderPagePng(0, 144); // rendered without the layerAttachments
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.
foreach (var a in JsonNode.Parse(doc.AttachmentsJson())!.AsArray())
Console.WriteLine($"{a!["name"]} {a["mime"]} {a["size"]} {a["page"]}");
File.WriteAllBytes("invoice.xml", doc.AttachmentData("invoice.xml"));
doc.AddAttachment("data.csv", File.ReadAllBytes("data.csv"), "text/csv", "Source data");
doc.RemoveAttachment("old.csv");
doc.Save("with-data.pdf");To attach a file to a page with a visible icon, use AddFileAttachmentAnnot; see
Annotations.
Related
- Editing: pages, text and images
- Opening and saving