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

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

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 (the default for each argument) keeps the current value.

use Nexdevit\NexPdf\Document;

$doc = Document::open('report.pdf');
$info = json_decode($doc->metadataJson(), true);
echo $info['title'] ?? '', ' ', $info['author'] ?? '', "\n";

$doc->setMetadata(title: 'Q3 report', author: 'Finance');
$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(array $items, int $depth = 0): void
{
    foreach ($items as $item) {
        printf("%s%s (page %d)\n", str_repeat('  ', $depth), $item['title'], $item['page'] + 1);
        walk($item['children'] ?? [], $depth + 1);
    }
}

walk(json_decode($doc->outlinesJson(), true));

$doc->setOutlinesJson(json_encode([
    ['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.

$labels = json_decode($doc->pageLabelsJson(), true);
print_r(array_slice($labels['labels'], 0, 4));   // 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.

foreach (json_decode($doc->layersJson(), true) as $layer) {
    echo $layer['name'], $layer['visible'] ? ' visible' : ' hidden', "\n";
    if ($layer['name'] === 'Notes') {
        $doc->setLayerVisible($layer['num'], $layer['gen'], 0);   // hide; 1 shows it again
    }
}
file_put_contents('page1.png', $doc->renderPagePng(0, 144));    // rendered without the layer

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.

foreach (json_decode($doc->attachmentsJson(), true) as $a) {
    echo $a['name'], ' ', $a['mime'], ' ', $a['size'], ' ', $a['page'] ?? 'document', "\n";
}

file_put_contents('invoice.xml', $doc->attachmentData('invoice.xml'));

$doc->addAttachment('data.csv', file_get_contents('data.csv'), 'text/csv', 'Source data');
$doc->removeAttachment('old.csv');
$doc->save('with-data.pdf');

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

On this page