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 React Native.

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

Metadata

metadata() resolves to the parsed document information dictionary (title, author, subject, keywords and the other entries the file has); metadataJson() gives the same as a JSON string. setMetadata(title, author, subject, keywords) changes it; null keeps the current value.

import { NexPdf } from '@nexdevit/nexpdf-react-native';

const doc = await NexPdf.open(reportBase64);
const info = await doc.metadata();
console.log(info.title, info.author);

await doc.setMetadata('Q3 report', 'Finance', null, null);
const titled = await doc.saveBytes();

Bookmarks

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

type Outline = { title: string; page: number; children?: Outline[] };

const outline: Outline[] = JSON.parse(await doc.outlinesJson());
const walk = (items: Outline[], depth = 0) => {
  for (const item of items) {
    console.log(`${'  '.repeat(depth)}${item.title} (page ${item.page + 1})`);
    walk(item.children ?? [], depth + 1);
  }
};
walk(outline);

await doc.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. pageLabelsJson() returns the label of every page and the ranges that produce them.

const labels = JSON.parse(await doc.pageLabelsJson());
console.log(labels.labels.slice(0, 4));     // e.g. ["i", "ii", "1", "2"]

await doc.setPageLabelsJson(JSON.stringify([
  { start: 0, style: 'roman' },
  { start: 2, style: 'decimal', first: 1 },
]));
await 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.

const layers = JSON.parse(await doc.layersJson());
for (const layer of layers) console.log(layer.name, layer.visible ? 'visible' : 'hidden');

const notes = layers.find((l: any) => l.name === 'Notes');
await doc.setLayerVisible(notes.num, notes.gen, 0);   // hide; 1 shows it again
const png = await doc.renderPage(0, 144);             // rendered without the layer

Attachments

PDFs can carry files: at document level (an attachment list) or on a page (a paperclip annotation). attachments() lists both; page is null for document-level files. File contents are base64.

for (const a of await doc.attachments()) console.log(a.name, a.mime, a.size, a.page);

const xmlBase64 = await doc.attachmentData('invoice.xml');

await doc.addAttachment('data.csv', csvBase64, 'text/csv', 'Source data');
await doc.removeAttachment('old.csv');
const withData = await doc.saveBytes();

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

On this page