PDF Tagging

Use PDF tags to define the physical structure or layout of the content within a PDF document. PDF tags help determine the reading order of different elements on a page. Tags are especially useful when a reading a PDF with a screen-reader device (often used by reading impaired users).

TagOptions

A Document is specified as tagged by creating a TagOptions object and setting it equal to the Document instance's Tag property. PDF Tagging is either done manually (where a specific type of tag is created and added to the Tag property of each PageElement) or done automatically (tags are generated automatically if the property is unspecified).

The simplest way to add tags to a Document is through its constructor.

// Create a document and set it's properties
Document document = new Document
{
     Creator = "HelloWorldTaggedPdf.aspx",
     Author = "ceTe Software",
     Title = "Hello World",

     // Specify document as tagged PDF
     Tag = new TagOptions()
};
' Create a document And set it's properties
' Specify document as tagged PDF
Dim MyDocument As Document = New Document With {
   .Creator = "HelloWorldTaggedPdf.aspx",
   .Author = "ceTe Software",
   .Title = "Hello World",
   .Tag = New TagOptions()
}

Refer to TaggedPdf on GitHub for a complete example (coresuite-dotne-core-framework).

StructureElement

Use the StructureElement class for more complex tagging of PDF documents.

Image Example

When tagging a document, all Images must provide text in the AlternateText property.

The following example demonstrates how to create a tagged PDF with a few page elements (an image and a page number label).

// Create a PDF document 
Document document = new Document();

// Specify document as a tagged PDF
document.Tag = new TagOptions(true);

// Create a page and add it to the document
Page page = new Page();
document.Pages.Add(page);

// Create an image
Image image = new Image(Server.MapPath("../Images/DPDFLogo.png"), 180f, 150f, 0.24f);
image.Height = 200;
image.Width = 200;

// Create a structure element 
StructureElement imageStructureElement = new StructureElement(TagType.Figure);
imageStructureElement.IncludeDefaultAttributes = true;
imageStructureElement.AlternateText = "DynamicPDF Logo";

// Set structure element to the image
image.Tag = imageStructureElement;

// Add image to the page
page.Elements.Add(image);

// Create a Page numbering label
PageNumberingLabel pageNumberingLabel = new PageNumberingLabel("Page %%CP%% of %%TP%%", 0, 680, 512, 12, Font.Helvetica, 12, TextAlign.Center);

// Create a artifact and add type
Artifact artifact = new Artifact();
artifact.SetType(ArtifactType.Pagination);

// Set artifact to the page numbering label
pageNumberingLabel.Tag = artifact;

// Add page numbering label to the page
page.Elements.Add(pageNumberingLabel);

// Outputs the document
document.Draw(pdfFilePath);        
' Create a PDF document 
Dim MyDocument As Document = New Document()

' Specify document as a tagged PDF
MyDocument.Tag = New TagOptions()

' Create a page and add it to the document
Dim MyPage As Page = New Page()
MyDocument.Pages.Add(MyPage)

' Create an image
Dim MyImage As Image = New Image(Server.MapPath("../Images/DPDFLogo.png"), 180.0F, 150.0F, 0.24F)
MyImage.Height = 200
MyImage.Width = 200

' Create a structure element 
Dim MyImageStructureElement As StructureElement = New StructureElement(TagType.Figure)
MyImageStructureElement.IncludeDefaultAttributes = True
MyImageStructureElement.AlternateText = "DynamicPDF Logo"

' Set structure element to the image
MyImage.Tag = MyImageStructureElement

' Add image to the page
MyPage.Elements.Add(MyImage)

' Create a Page numbering label
Dim MyPageNumberingLabel As PageNumberingLabel = New PageNumberingLabel("Page %%CP%% of %%TP%%", 0, 680, 512, 12, Font.Helvetica, 12, TextAlign.Center)

' Create a artifact and add type
Dim MyArtifact As Artifact = New Artifact()
MyArtifact.SetType(ArtifactType.Pagination)

' Set artifact to the page numbering label
MyPageNumberingLabel.Tag = MyArtifact

' Add page numbering label to the page
MyPage.Elements.Add(MyPageNumberingLabel)

' Outputs the document
MyDocument.Draw(pdfFilePath)

In this topic