Pagecraft

bookmarks

Add PDF bookmarks

Give a document an outline a reader can navigate it by.

Reads
a PDF — application/pdf
Writes
one file — PDFs
Tool page
/add-bookmarks-to-pdf

Example

Give a report a contents panel a reader can navigate it by.

Given

  • report.pdf — an ordinary PDF of 6 pages

From code

            import { loadDocument, runOperation } from '@pagecraft/core';

const report = await loadDocument({ bytes, name: 'report.pdf' });

const [output] = await runOperation('bookmarks', [report], {
  entries: [
    {
      title: 'Summary',
      page: 1,
      level: 1,
    },
    {
      title: 'Findings',
      page: 2,
      level: 1,
    },
    {
      title: 'Method',
      page: 4,
      level: 2,
    },
  ],
  replace: true,
});
          

From a shell

            pagecraft bookmarks report.pdf --entries '[{"title":"Summary","page":1,"level":1},{"title":"Findings","page":2,"level":1},{"title":"Method","page":4,"level":2}]'
          

What comes back

One PDF of 6 pages.

This example is run by the test suite. If it stopped being true, the build would fail.

Options

entries--entries

object[]

Bookmarks

Each heading, the page it goes to, and how far it is indented.

replace--replace

boolean

Replace the existing bookmarks

Off adds these below the ones the document already has.

Default: true

What it cannot do

  • A bookmark goes to the top of a page. The zoom and scroll position an author chose for one of the document’s own bookmarks is kept; a bookmark added here has none.
  • Levels are followed, not checked. A level that skips a rank — a 1 followed by a 3 — is taken in one rank rather than refused, because a reader’s sidebar cannot show the difference.
  • This writes the outline a reader’s sidebar shows. It draws no contents page into the document itself.
  • Replacing the bookmarks removes every one the document had, including any that pointed at a named destination rather than at a page.