Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Record your own metadata

You’ll record a review in a PDF’s XMP metadata: its status, its reviewers and the review round, as properties of your own. You need this when a workflow tracks facts a PDF has no field for, such as an approval, a case number or a retention date.

XMP metadata is the XML copy of a PDF’s metadata. Besides the title and authors, it holds any property a namespace defines, and a namespace is a URI you own, so your names can’t clash with anyone else’s. This example edits an archive-ready PDF/A file:

packages/sdk/examples/edit-xmp.ts
// Record a review in a document's XMP metadata: properties of your own, beyond the title and
// authors, which a PDF/A file keeps without losing its claim.
// Each property is named by a namespace you own and a local name. In a PDF/A file, Lamina PDF also
// declares each new property in the metadata's extension schemas, as PDF/A requires, and the edit
// lists what it declared.
import { Document } from "@laminapdf/sdk";
/** The namespace of the review properties: a URI you own, so they can't clash with anyone else's. */
const REVIEW = "https://example.com/ns/review/1.0/";
/**
* The document with the review recorded in its XMP metadata, saved incrementally.
*
* @param savedAt - the time of the save, as a PDF date (`D:20261110093000Z`) your app takes
* from its clock.
*/
export function editXmp(bytes: Uint8Array, savedAt: string): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
const reviewers = [
{ type: "text", value: "Ada Lovelace" },
{ type: "text", value: "Charles Babbage" },
] as const;
const edits = [
doc.setXmpProperty(REVIEW, "review", "status", { type: "text", value: "Approved" }),
doc.setXmpProperty(REVIEW, "review", "reviewers", { type: "seq", value: [...reviewers] }),
doc.setXmpProperty(REVIEW, "review", "round", { type: "integer", value: 2 }),
];
for (const property of edits.flatMap((edited) => edited.declared)) {
console.warn(`declared ${property.prefix}:${property.name} as ${property.valueType}`);
}
return doc.save({ mode: "incremental", metadataDate: savedAt });
}
Page 1 of edit_xmp.pdf
Open edit_xmp.pdf (1 page, 18 KB)

The result is the original bytes followed by one revision. Its XMP metadata holds review:status, review:reviewers and review:round, and still meets PDF/A-2b.

  • setXmpProperty(namespace, prefix, name, value) (SetXmpProperty in Rust) sets one property. prefix is the short name the XML writes for the namespace, used when the metadata has none for it yet.
  • A value carries its type: { type: "text", value: "Approved" }. The types are text, uri, integer, real, boolean, date (a PDF date), langAlt (the same text in several languages), and seq and bag, ordered and unordered lists of one type.
  • PDF/A allows only the properties its standard names, unless the metadata declares the others in an extension schema. Lamina PDF writes that declaration for each new property, and the edit’s declared lists them.
  • A file without XMP metadata gets it with the first property you set.
  • Remove a property: removeXmpProperty(namespace, name). As with any removal, an incremental save keeps the old value in the file’s earlier revisions; save with mode: "fullRewrite" to drop it.
  • Title, authors and dates: use setMetadata. setXmpProperty on dc:title works too and keeps the Info dictionary in step, but only with the value type the field needs: text for the title, a seq of text for the authors.
  • Structures and comments: read the metadata with xmpPacket(), change its XML, and write it back with replaceXmpPacket(xml). XML that doesn’t parse is refused with invalid-xmp-packet, and nothing changes.
  • The PDF/A or PDF/UA claim itself: an edit can’t change it (xmp-claim-read-only). Only converting or checking the file makes that claim.

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