Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Choose how a PDF opens

You’ll make a PDF open on a chosen page at the page’s width, with its bookmarks panel showing and its title in the window. You need this for a report that opens on its summary, or a manual that shows its contents first.

A PDF keeps these choices in its catalog, and every reader that follows the PDF standard reads them when it opens the file. One edit sets them:

packages/sdk/examples/set-initial-view.ts
// Choose how a PDF opens: on a given page at the page's width, with its bookmarks shown and its title in the window.
// The edit changes only what the file asks a reader to do when it opens it. A file whose open
// action runs a script keeps that action: the edit leaves the opening page alone and changes the
// rest.
import { Document, type InitialViewPatch } from "@laminapdf/sdk";
/**
* The document set to open on page `index` (0-based) at the page's width, with the bookmarks
* panel and the document's title in the window, saved incrementally. Needs
* `init({ extract: true })`.
*/
export function openOn(bytes: Uint8Array, index: number): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
const view: InitialViewPatch = { pageMode: "outlines", preferences: { displayDocTitle: true } };
// An open action that runs a script or another action is never replaced, so leave the page out.
const open = doc.initialView().open;
if (open.kind === "action") {
console.warn(`the file runs a ${open.action} action when it opens; keeping it`);
} else {
view.open = { page: doc.page(index).id, zoom: "fitWidth" };
}
doc.setInitialView(view);
return doc.save({ mode: "incremental" });
}

The result is the original bytes, unchanged, followed by one revision. The file opens on the page you chose, fitted to the window’s width, with the bookmarks panel open and the document’s title in the window’s title bar.

  • initialView() reads how the file asks to open. Its open is "default" when the file leaves the first page and zoom to the reader, "page" with a target in page space, or "action" when the file runs an action, such as a script, instead.
  • setInitialView (SetInitialView in Rust) takes a patch: a value sets a field, null removes it, and a field you leave out stays as the file has it.
  • open names the page by its ID and a zoom: "fitWidth", "fitPage", "default" (the reader’s own zoom) or a percentage from 1 to 6400.
  • pageMode is the panel beside the page: "outlines" for bookmarks, "thumbnails", "attachments", "optionalContent" for layers, "fullScreen" or "none".
  • preferences holds the window’s requests, such as displayDocTitle for the title in the title bar. true sets one; false removes it, which is its default.
  • An open action that runs anything besides opening a page (a script, a named action, a page change followed by further actions) is never replaced: an edit that names the page fails with open-action-not-destination. The example reads the file’s view first and leaves the page out of its edit then. Lamina PDF never runs the action.
  • When a value needs a newer PDF version than the file declares, such as the attachments panel (PDF 1.6), the edit raises the file’s version to it.
  • Another page layout: add pageLayout, such as "twoPageRight" for two pages side by side with the cover alone.
  • The reader’s defaults again: pass null for each field you set, open included.
  • The article threads: threads() lists a file’s article threads, each with its parts as rectangles in page space, in reading order.

Next: save a PDF, for what a signed or repaired file allows.