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

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 decode JSON results with encoding/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; "" keeps the current value.

doc, err := nexpdf.Open("report.pdf")
if err != nil {
	log.Fatal(err)
}
defer doc.Close()

s, _ := doc.MetadataJSON()
var info map[string]any
json.Unmarshal([]byte(s), &info)
fmt.Println(info["title"], info["author"])

doc.SetMetadata("Q3 report", "Finance", "", "")
doc.Save("report-titled.pdf", false)

Bookmarks

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

type Bookmark struct {
	Title    string     `json:"title"`
	Page     int        `json:"page"`
	Children []Bookmark `json:"children,omitempty"`
}

func walk(items []Bookmark, depth int) {
	for _, b := range items {
		fmt.Printf("%s%s (page %d)\n", strings.Repeat("  ", depth), b.Title, b.Page+1)
		walk(b.Children, depth+1)
	}
}

s, _ := doc.OutlinesJSON()
var outline []Bookmark
json.Unmarshal([]byte(s), &outline)
walk(outline, 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.

var labels struct {
	Labels []string `json:"labels"`
}
s, _ := doc.PageLabelsJSON()
json.Unmarshal([]byte(s), &labels)
fmt.Println(labels.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.

type Layer struct {
	Num     uint32 `json:"num"`
	Gen     uint16 `json:"gen"`
	Name    string `json:"name"`
	Visible bool   `json:"visible"`
}

s, _ := doc.LayersJSON()
var layers []Layer
json.Unmarshal([]byte(s), &layers)
for _, l := range layers {
	fmt.Println(l.Name, l.Visible)
	if l.Name == "Notes" {
		doc.SetLayerVisible(l.Num, l.Gen, 0) // hide; 1 shows it again
	}
}
png, _ := doc.RenderPagePNG(0, 144) // rendered without the layer
os.WriteFile("page1.png", png, 0o644)

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.

type Attachment struct {
	Name string `json:"name"`
	Mime string `json:"mime"`
	Size int    `json:"size"`
	Page *int   `json:"page"`
}

s, _ := doc.AttachmentsJSON()
var list []Attachment
json.Unmarshal([]byte(s), &list)
for _, a := range list {
	fmt.Println(a.Name, a.Mime, a.Size, a.Page != nil)
}

xml, err := doc.AttachmentData("invoice.xml")
if err == nil {
	os.WriteFile("invoice.xml", xml, 0o644)
}

csv, _ := os.ReadFile("data.csv")
doc.AddAttachment("data.csv", csv, "text/csv", "Source data")
doc.RemoveAttachment("old.csv")
doc.Save("with-data.pdf", false)

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

On this page