Skip to Content
NexPDF

Document structure

Read and set metadata, bookmarks and page labels, show and hide layers, and embed or extract file attachments of the document in a .NET MAUI PdfView.

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

PdfView has no outline or attachment panel; read the structure from view.Document and build the UI you need. The results are JSON strings; the examples parse them with System.Text.Json.

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;

var doc = view.Document!;
var info = JsonNode.Parse(doc.MetadataJson())!;
Title = info["title"]?.GetValue<string>() ?? "Untitled";

doc.SetMetadata(title: "Q3 report", author: "Finance");

Bookmarks

Bookmarks (the outline) are a JSON tree of title, 0-based page and optional children. A table of contents only needs the titles and pages; GoToPageAsync jumps there:

record Bookmark(string Title, int Page, int Depth);

static void Flatten(JsonArray items, int depth, List<Bookmark> into)
{
    foreach (var item in items)
    {
        into.Add(new Bookmark(item!["title"]!.GetValue<string>(), item["page"]!.GetValue<int>(), depth));
        if (item["children"] is JsonArray children) Flatten(children, depth + 1, into);
    }
}

var bookmarks = new List<Bookmark>();
Flatten(JsonNode.Parse(view.Document!.OutlinesJson())!.AsArray(), 0, bookmarks);
OutlineList.ItemsSource = bookmarks;

// when the user taps a bookmark:
await view.GoToPageAsync(selected.Page);

SetOutlinesJson replaces the whole tree:

view.Document!.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, which you can show instead of the page index:

var labels = JsonNode.Parse(view.Document!.PageLabelsJson())!["labels"]!.AsArray();
view.PageChanged += (_, page) =>
    Title = page < labels.Count ? $"Page {labels[page]}" : $"Page {page + 1}";

view.Document.SetPageLabelsJson("""[{"start":0,"style":"roman"},{"start":2,"style":"decimal","first":1}]""");

Styles are decimal, roman, ROMAN, letters, LETTERS and none. A range can also have a prefix, for example "A-". SetPageLabelsJson("[]") 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. After you switch a layer, call ReloadAsync() so the view renders the pages again:

var doc = view.Document!;
var notes = JsonNode.Parse(doc.LayersJson())!.AsArray()
    .First(l => l!["name"]!.GetValue<string>() == "Notes")!;

doc.SetLayerVisible(notes["num"]!.GetValue<uint>(), notes["gen"]!.GetValue<ushort>(), 0);   // hide; 1 shows it again
await view.ReloadAsync();

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. To open one in another app, write it to the cache folder and share it:

var doc = view.Document!;
foreach (var a in JsonNode.Parse(doc.AttachmentsJson())!.AsArray())
    System.Diagnostics.Debug.WriteLine($"{a!["name"]} {a["mime"]} {a["size"]} {a["page"]}");

var path = Path.Combine(FileSystem.CacheDirectory, "invoice.xml");
await File.WriteAllBytesAsync(path, doc.AttachmentData("invoice.xml"));
await Share.Default.RequestAsync(new ShareFileRequest("Attachment", new ShareFile(path)));

doc.AddAttachment("data.csv", csvBytes, "text/csv", "Source data");
doc.RemoveAttachment("old.csv");

To attach a file to a page with a visible icon, use AddFileAttachmentAnnot; see the .NET annotations guide.

On this page