Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Place images and shapes on a page

You’ll brand a report: a pale band behind its title, the company’s logo over the band’s right end, and its wordmark at the foot of the page. You need this to letterhead documents, put a logo on generated reports, or let people place pictures and shapes on pages.

Each object becomes part of the page, as if the page had been made with it. Unlike a stamp or a comment, it prints, every reader draws it, and no one can hide it. The code adds a band, a logo and a wordmark to the report’s first page:

packages/sdk/examples/place-content.ts
// Brand a report: a pale band behind its title, the company's logo over the band's right end,
// and its wordmark, a page of another PDF kept as vectors and text, at the foot of the page.
// Each is placed as page content, not as a comment: it prints and every reader draws it. The
// boxes are in page space (points from the page's top-left), as a viewer reports where a
// pointer went. In this tagged report the logo and the wordmark are figures with alternate
// text, and the band is decoration, which screen readers skip.
import { Document } from "@laminapdf/sdk";
// Where each goes on the example's input, the quarterly report, in points from the page's
// top-left: for your own file, take them from your viewer's pointer.
/** The band behind the title: `[x0, y0, x1, y1]`. */
const BAND = [44, 68, 551, 110] as const;
/** The logo, over the band's right end, as wide again as it is tall, as the image is. */
const LOGO = [455, 65, 551, 113] as const;
/** The wordmark, at the foot of the page, the size of its page. */
const WORDMARK = [56, 740, 176, 800] as const;
/** The matrix that stretches an image's or a page's unit square to the box `[x0, y0, x1, y1]`. */
function to([x0, y0, x1, y1]: readonly [number, number, number, number]) {
return [x1 - x0, 0, 0, y1 - y0, x0, y0] as const;
}
/**
* The document branded: a band, a logo and a wordmark on its first page, saved incrementally.
* `at` is when it was branded, as a PDF date.
*/
export function placeContent(
bytes: Uint8Array,
logo: Uint8Array,
wordmark: Uint8Array,
at: string,
): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
// Behind the page's own content, so the title stays on top of it.
doc.content.add(0, {
id: "title-band",
object: {
shape: { path: { rect: { rect: [...BAND], radius: 6 } }, fill: [0.89, 0.93, 0.98] },
},
matrix: [1, 0, 0, 1, 0, 0],
layer: "behind",
alt: "decorative",
modified: at,
});
doc.content.add(0, {
id: "logo",
object: { image: { blob: doc.addBlob(logo) } },
matrix: to(LOGO),
alt: { text: "Northwind Traders" },
modified: at,
});
doc.content.add(0, {
id: "wordmark",
object: { form: { blob: doc.addBlob(wordmark), page: 0 } },
matrix: to(WORDMARK),
alt: { text: "Northwind, the company's wordmark" },
modified: at,
});
return doc.save({ mode: "incremental" });
}
Page 1 of place_content.pdf
Open place_content.pdf (2 pages, 28 KB)

The result is the original bytes, unchanged, followed by one revision. The band sits behind the title, the logo’s transparent PNG edges let the band show through at its right end, and the wordmark stays sharp at any zoom because it is still vectors and text.

  • You name each object with an ID of your own, unique on its page. Lamina PDF stores the ID with the object, so content.update and content.remove find it again after the file is saved and reopened, and content.added lists what a page holds.
  • An object’s matrix places it on the page, in page space: points from the page’s top-left corner as shown, with y going down. A picture or a page of another PDF fills a square one point wide, so the matrix [w, 0, 0, h, x, y] draws it w by h points with its top-left corner at (x, y). A shape’s points are already in page space, so the identity matrix [1, 0, 0, 1, 0, 0] draws it where they are. Turn or mirror an object by changing the matrix.
  • Lamina PDF never rewrites the page’s own content. It draws each object with a few drawing instructions of its own, added after the page’s content, or before it for the behind layer. The page’s text and pictures keep their bytes, and an object moves without touching anything else.
  • In a tagged file, an object with alternate text becomes a figure that screen readers announce, and one marked "decorative" is skipped. A file that claims PDF/UA-1 requires one or the other for a picture, and refuses it otherwise with content-needs-alt-text.
  • A shape: give its path as { line }, { rect } (with an optional corner radius), { ellipse } or { polygon }, filled with fill, outlined with stroke, or both. A stroke’s dash is "solid", "dashed" or "dotted".
  • Move, resize or turn an object later: content.update(index, id, { matrix, modified }). The same call takes opacity, an image’s crop (the part of its square that shows), a shape’s fill and stroke, and new bytes in blob. Its order is the object’s place among those on its layer: 0 sends it to the back and -1 brings it to the front.
  • Fade it: give opacity from 0 to 1. A file that claims PDF/A-1 forbids see-through objects, so doc.plan() warns with pdfa-transparency; later PDF/A parts allow them.
  • Remove it: content.remove(index, id). An incremental save keeps the file’s earlier revisions, and the object stays in them; save with mode: "fullRewrite" to write the file without it.
  • Image formats: JPEG and PNG work, and a PNG keeps its transparency. Convert other formats first; anything else fails with content-object-invalid.
  • Protected PDFs: some PDFs need a password or forbid copying. To place a page of one, call addSource with its bytes and the password; only the permissions password lifts a limit. Then use the ID it returns as the object’s blob. The password stays out of the edit and out of the saved file. See owner-password-required.
  • In the browser: placing objects works in every module, the base module included.

Next: edit what a page draws, to move and change the objects a page already had.