PDF Tagging

Use PDF tags to define the physical structure or layout of the content within a PDF document. PDF tags can help with the specific reading order of different elements on the page. This is especially useful when the PDF is read by a screen-reader device (which may be used by blind or disabled users).

Within the DynamicPDF API, a Document can be specified as tagged by creating a TagOptions object and setting that equal to the Document's Tag property. PDF Tagging can either be done manually (where a specific type of tag is created and added to the Tag property of each PageElement) or tags can be automatically generated by DynamicPDF (tags will be generated automatically if nothing has been specified for a Page Elements Tag property). Additionally, all Images must provide text in the AlternateText property.

The following example demonstrates how to create a tagged PDF with a few simple page elements (an image and a page number label). For an example that demonstrates even more tagged elements on a PDF document, take a look at the "TaggerPdfWithStructureElements" example included with our product's download.

// 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