Document structure
Read and set metadata, bookmarks and page labels, show and hide layers, and embed or extract file attachments in the browser with NexPDF for the web.
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).
Apart from metadata(), these 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 { loadNexPdf } from '@nexdevit/nexpdf-web/engine';
const nex = await loadNexPdf();
const doc = await nex.PdfDocument.fromUrl('/report.pdf');
const info = doc.metadata();
console.log(info.title, info.author);
doc.raw.setMetadata('Q3 report', 'Finance', null, null);
const titled = doc.toBlob();Bookmarks
Bookmarks (the outline) are a JSON tree of title, 0-based page and optional children.
setOutlinesJson replaces the whole tree.
type Bookmark = { title: string; page: number; children?: Bookmark[] };
const outline: Bookmark[] = JSON.parse(doc.raw.outlinesJson());
const walk = (items: Bookmark[], depth = 0) => {
for (const item of items) {
console.log(`${' '.repeat(depth)}${item.title} (page ${item.page + 1})`);
walk(item.children ?? [], depth + 1);
}
};
walk(outline);
doc.raw.setOutlinesJson(JSON.stringify([
{ title: 'Summary', page: 0 },
{ title: 'Details', page: 2, children: [{ title: 'Costs', page: 3 }] },
]));Use the tree to build a table of contents next to your rendered pages.
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.
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
img.src = doc.renderUrl(0, { dpi: 110 }); // 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.
for (const a of JSON.parse(doc.raw.attachmentsJson())) {
console.log(a.name, a.mime, a.size, a.page);
}
const xml = doc.raw.attachmentData('invoice.xml'); // Uint8Array
const link = Object.assign(document.createElement('a'), {
href: URL.createObjectURL(new Blob([xml], { type: 'application/xml' })),
download: 'invoice.xml',
});
link.click();
const csv = new Uint8Array(await csvFile.arrayBuffer());
doc.raw.addAttachment('data.csv', csv, 'text/csv', 'Source data');
doc.raw.removeAttachment('old.csv');
const withData = doc.toBlob();To attach a file to a page with a visible icon, use addFileAttachmentAnnot; see
Annotations.
Related
- Editing: pages, text and images
- Opening and saving