Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Edit a tagged PDF

You’ll edit an accessible report that claims PDF/UA and save it with its claim still true. Lamina PDF keeps the file’s tags up to date as you edit, so screen readers still read the page in its order, and before you save it tells you which claims your edits broke.

The program deletes the report’s summary paragraph, renames its table’s heading and rewrites the alternative text of its chart. It then asks the save plan which claims no longer hold, and removes only those claims before saving:

packages/sdk/examples/edit-tagged-pdf.ts
// Edit an accessible (tagged, PDF/UA) document and keep its claim, or save without it.
//
// Deletes the summary paragraph of the report from `pdf-ua-report`, renames its table's heading
// and rewrites the alternative text of its chart. Lamina PDF keeps the tags in step: the deleted
// paragraph leaves the structure tree, and the heading's element still finds its text. Before
// saving, the plan lists the PDF/A and PDF/UA claims the edits broke; this example removes only
// those claims, so the file doesn't promise what it no longer meets.
import { Document } from "@laminapdf/sdk";
/** The report with its summary deleted, its table's heading renamed and its chart described anew. Needs `init({ extract: true })`. */
export function editTaggedPdf(bytes: Uint8Array): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
const runs = doc.content.textRuns(0);
const summary = runs.find((r) => r.text.startsWith("Sales grew"));
const heading = runs.find((r) => r.text === "Sales by quarter");
if (!summary || !heading) {
throw new Error("not the report from pdf-ua-report");
}
// Each edit names its run as the page is before it: the heading comes later on the page, so
// renaming it first leaves the summary's name as it was.
doc.content.editText(0, heading.run, "Quarterly sales");
// Empty text deletes the run, and the paragraph it was.
doc.content.editText(0, summary.run, "");
// The chart is the object that already has alternative text. Name it after the edits above,
// since deleting the summary renumbers the page's objects. Setting its text changes it for
// every object of the chart.
const chart = doc.content.objects(0).find((o) => o.alt !== undefined);
if (!chart) {
throw new Error("no chart");
}
doc.content.setAlt(0, chart.object, "Bar chart of quarterly sales: 120, 135, 160 and 150 thousand euros.");
// A `pdfa-…` code (and `imported-pdfa-unchecked`) breaks the PDF/A claim; the others, the
// PDF/UA claim.
const broken = doc.plan().conformanceWarnings.map((w) => w.code);
const pdfa = broken.some((code) => code.includes("pdfa"));
const pdfua = broken.some((code) => !code.includes("pdfa"));
if (pdfa || pdfua) {
console.warn(`the edits broke a claim: ${broken.join(", ")}`);
doc.removeConformanceClaims({ pdfa, pdfua });
}
return doc.save();
}
Page 1 of edit_tagged_pdf.pdf
Open edit_tagged_pdf.pdf (1 page, 13 KB)

In the result, the summary is gone, the heading reads “Quarterly sales” and a screen reader describes the chart with the new text. The file still meets PDF/UA-1, so it keeps its claim.

A tagged PDF marks each piece of a page’s content with a number, and its structure tree says which element (a heading, a paragraph, a figure) each number belongs to. Lamina PDF updates both as you edit:

  • Deleted content leaves the tree. Delete an object or a run of text, and its element no longer names it. An element left with no content leaves the tree too.
  • Moved and rewritten content keeps its place. An object you move, resize or reorder, and text you rewrite, stay in their elements.
  • Text alternatives follow the text. When you rewrite text whose element has a text alternative (ActualText or Alt) that holds the old words once, the new words replace them. An alternative that doesn’t hold them is removed, so a screen reader reads the new text itself.
  • You can describe a figure yourself. doc.content.setAlt() sets the alternative text of the figure that owns an object, and doc.content.objects() reports the text an object’s figure has now as alt. The text belongs to the figure, so every object of a chart drawn as many paths shares it.
  • A replaced figure needs new alternative text. The old text described the old image, so Lamina PDF removes it, and in a file that claims PDF/UA-1 the save plan warns with figure-without-alt until the figure has new text.

doc.plan().conformanceWarnings lists each PDF/A or PDF/UA claim the edits break. The warnings never block a save, so you choose what to do:

  • Fix it first: undo the edit that broke the claim. For a replaced figure, you can instead add alternative text with an accessibility tool and open the file again. The warning goes when the problem does.
  • Save without the claim: doc.removeConformanceClaims({ pdfua: true }) takes the claim out of the file’s XMP metadata, so the file no longer promises what it doesn’t meet. Set only the claims the warnings name, as the example does. This edit is the only one that may remove a claim; the other metadata edits refuse with xmp-claim-read-only. An incremental save keeps the old claim in the file’s earlier revisions; save with mode: "fullRewrite" to drop it.

Content edits can break a claim three ways: figure-without-alt for a replaced figure, content-tags-broken for content the page’s tags don’t cover, and pdfa-font-not-embedded for a font an edit added without embedding it (once edits can add fonts).

Next: edit the text a page shows, or a PDF/UA report to make an accessible file from the start.