Document structure
Read and set metadata, bookmarks and page labels, show and hide layers, and embed or extract file attachments with NexPDF for Node.js.
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).
metadata() and outlines() return parsed objects. The other calls are on doc.raw and take and
return JSON strings; parse them with JSON.parse.
Metadata
metadata() returns the document information dictionary (title, author, subject, keywords and the
other entries the file has). doc.raw.setMetadata(title, author, subject, keywords) changes it;
null keeps the current value.
import { PdfDocument } from '@nexdevit/nexpdf-node';
const doc = await PdfDocument.open('report.pdf');
const info = doc.metadata();
console.log(info.title, info.author);
doc.raw.setMetadata('Q3 report', 'Finance', null, null);
await 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.
function walk(items, depth = 0) {
for (const item of items) {
console.log(`${' '.repeat(depth)}${item.title} (page ${item.page + 1})`);
walk(item.children ?? [], depth + 1);
}
}
walk(doc.outlines());
doc.raw.setOutlinesJson(JSON.stringify([
{ 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. pageLabels() returns the label of every page and the ranges that produce them, and
setPageLabels(ranges) replaces the ranges ([] removes the labels).
const { labels, ranges } = doc.pageLabels();
console.log(labels.slice(0, 4)); // e.g. ["i", "ii", "1", "2"]
doc.setPageLabels([{ start: 0, style: 'roman' }, { start: 2, style: 'decimal', first: 1 }]);
doc.setPageLabels([]); // removes the labelsStyles are decimal, roman, ROMAN, letters, LETTERS and none. A range can also have a
prefix, for example "A-". The raw calls are doc.raw.pageLabelsJson() and
doc.raw.setPageLabelsJson(json).
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.
import { writeFile } from 'node:fs/promises';
const layers = JSON.parse(doc.raw.layersJson());
for (const layer of layers) console.log(layer.name, layer.visible ? 'visible' : 'hidden');
const notes = layers.find((l) => l.name === 'Notes');
doc.raw.setLayerVisible(notes.num, notes.gen, 0); // hide; 1 shows it again
await writeFile('page1.png', doc.render(0, { dpi: 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.
import { readFile, writeFile } from 'node:fs/promises';
for (const a of JSON.parse(doc.raw.attachmentsJson())) {
console.log(a.name, a.mime, a.size, a.page);
}
await writeFile('invoice.xml', doc.raw.attachmentData('invoice.xml'));
doc.raw.addAttachment('data.csv', await readFile('data.csv'), 'text/csv', 'Source data');
doc.raw.removeAttachment('old.csv');
await 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