As of version 5.0 of PDFKit.NET, you can create tagged PDF documents. This code sample shows how to create a tagged PDF from scratch.

What is a tagged PDF?

A tagged PDF has additional information that is referred to as the logical structure of the PDF. The logical structure is a hierarchy of structure elements that exists separately from the visual content. The logical structure is associated with the visual content by bi-directional pointers. A piece of visual content is said to by tagged if such a pointer exists and the corresponding structure element is referred to as the tag.

The following sections demonstrate the tagged PDF API.

Initialize logical structure

The following code initializes the logical structure:

Document document = new Document();

LogicalStructure logicalStructure = new LogicalStructure();
document.LogicalStructure = logicalStructure;

document.Pages.Add(new Page(600, 800));
Page page = document.Pages[0];

DocumentTag documentTag = new DocumentTag(logicalStructure.Root);
SectionTag sectionTag = new SectionTag(documentTag);

Note how the section tag is added as a child of the root tag by passing the parent tag to the constructor.

Tag an image

ImageShape image = new ImageShape("logo_pdfkit.gif");
image.Transform = new TranslateTransform(480, 710);
FigureTag imageTag = new FigureTag(sectionTag);
image.Tag = imageTag;
imageTag.AlternateDescription = "TallComponents logo";

Tag an image caption

TextShape caption = new TextShape(470, 695, "by TallComponents", new TallComponents.PDF.Fonts.Font(), 10);
caption.Tag = new CaptionTag(imageTag);

Tag a heading

TextShape heading = new TextShape(50, 650, "Creating tagged PDF", new TallComponents.PDF.Fonts.Font(), 20);
heading.Tag = new HeadingNTag(1, sectionTag);

Tag a text paragraph

MultilineTextShape exampleText = new MultilineTextShape(50, 640, 500);
exampleText.FirstLineIndentation = 50;
exampleText.Fragments.Add(new Fragment("A simple demonstration how to create tagged document."));
exampleText.Fragments.Add(new Fragment("Above is the tagged logo of TallComponents and a tagged heading."));
exampleText.Fragments.Add(new Fragment("Next are some more examples for MultilineTextShape and SimpleXHtmlShape."));
exampleText.Tag = new ParagraphTag(sectionTag);

Editing the role map

The Tagged PDF conventions list standard roles for tags such as . It is possible to introduce application specific roles. If you do, you should also provide mappings from these custom roles to the nearest standard roles. This will help tools that process tagged PDF to deal with your roles in the best way possible. This can be done as follows:

logicalStructure.RoleMap["paragraph"] = "myparagraph";

Write the PDF

using (FileStream file = new FileStream("mytaggedpdf.pdf", FileMode.Create))