Tagging existing documents
editTag a document
Most documents in circulation are not tagged: screen readers cannot tell their headings from their paragraphs, and they cannot be converted to PDF/A level a. AutoTag finds the layout of the pages and tags the document:
var document = PdfDocument.Open("untagged-report.pdf");
var result = document.AutoTag(new PdfAutoTagOptions
{
Language = "en-US",
FigureAlternateText = image => "Company logo",
});- The structure tree gets a
Documentelement with the headings (H1toH6), the paragraphs (P), the lists (L,LI,LBody), the tables (Table,TR,THfor the first row,TD) and the figures, in reading order. - The content of the pages is rewritten: each operator that shows text or an image is in a marked-content sequence of its element. The headers, the footers, the rules and the shapes are artifacts. Only marked-content operators are added: the pages look the same.
- FigureAlternateText gives the alternate description of each image, which PDF/UA requires; the result counts the figures without one (FiguresWithoutAlternateText).
- A document that is already tagged throws
InvalidOperationException.
Then check the result with PdfAccessibilityInspector (Inspecting accessibility), correct the elements of StructureTree when needed, and convert the document to PDF/A level a (Converting to PDF/A).
How the layout is found
PdfDocument.ExtractLayout returns the layout without tagging, and PdfPage.ExtractLayout the layout of one page: its PdfLayoutNode objects, in reading order.
| Node | Found from |
|---|---|
| Heading | A block of at most three lines in a font larger than the body text (the size of most of the text), or a bold line of that size. The levels follow the sizes, the largest first |
| List, ListItem | Lines that start with a bullet, a dash, or a number or a letter followed by a period or a parenthesis |
| Paragraph | The other blocks of text: the blocks of the reading order of the text extraction (Read the columns in order) |
| Table, TableCell | A grid of rules, read by PdfTableExtractor |
| Figure | An image the page paints |
| Header, Footer | Text repeated at the same place in the top or bottom 15% of half the pages or more; the numbers in it may differ (page numbers) |
PdfLayoutOptions turns the detection of the headers, footers and tables off, and sets the size ratio of the headings.
Limits
- The layout is a guess from the positions and the fonts of the text: a document whose headings are in the font of the body, or whose tables have no rules, is tagged with paragraphs.
- A text operator is in one element: a
TJoperator that shows the text of two blocks is in the element of the first one, and a form XObject is in the element of its first text. - The structure follows the reading order; the content is not reordered, so an inspection may report a reading order that differs from the order of the content.