Create Tagged PDFs in C#
This guide explains how to create a valid tagged PDF document from scratch. A tagged PDF has a logical structure that makes the document more accessible. For example, you can use tags to make PDF documents easier to process with screen reader software.
To create a tagged PDF document, follow these steps:
-
Create a
GdPicturePDF
object. -
Set the measurement unit with the
SetMeasurementUnit
method. This method takes a member of thePdfMeasurementUnit
enumeration as its parameter. For example, to set the unit of measurement to millimeters, callgdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter)
. -
Set the origin of the coordinate system with the
SetOrigin
method. This method takes a member of thePdfOrigin
enumeration as its parameter. For example, to set the origin to the top-left corner, callgdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
. -
Create a new PDF document with the
NewPDF
method. Optionally, specify the conformance level with a member of thePdfConformance
enumeration. -
Add a new A4-sized page to the document with the
NewPage
method. This method takes a member of thePdfPageSizes
enumeration as its parameter. -
Set the font type to be used in the PDF document with the
AddTrueTypeFontU
method, and store the font resource name in a variable. -
Set the font size in points with the
SetTextSize
method. For more information, see Text Settings in PDFs. -
Load the image to be used in the PDF document with the
AddJpegImageFromFile
method. -
Set the title of the document with the
SetTitle
method. This title isn’t visually displayed in the document, but it’s required for some PDF conformance levels, such as PDF/UA. -
Set the language with the
SetLanguage
method. -
Determine the ID of the root tag with the
GetTagRootID
method. -
Create a section tag for the document content with the
NewTag
method. This method takes the ID of the parent tag and the type of the newly created tag as its parameters. In this case, the type of the new tag isSect
. -
Add text to the new section with the following methods:
-
Start adding marked content with the
BeginMarkedContentSequence
method. This method takes the ID of the parent tag and the type of the newly created marked content as its parameters. In this case, the type isSpan
. -
Add text with the
DrawText
method. This method takes the following parameters: the variable for the font type, the coordinates specifying where to add the text, and the text to be added. You can define single-line or multiline text. -
Finish adding marked content with the
EndMarkedContent
method.
-
-
Create a figure tag for the image with the
NewTag
method. This method takes the ID of the parent tag and the type of the newly created tag as its parameters. In this case, the type of the new tag isFigure
. -
Set an alternate description to the image with the
SetTagAlternateDescription
method. -
Add the image to the new figure tag with the following methods:
-
Start adding marked content with the
BeginMarkedContentSequence
method. This method takes the ID of the parent tag and the type of the newly created marked content as its parameters. In this case, the type isFigure
. -
Add the image with the
DrawImage
method. This method takes the following parameters: the image ID, the coordinates of the image’s closest point to the origin, and the width and the height of the image. The coordinates and the lengths are relative to the origin and expressed in the measurement unit defined above. -
Finish adding marked content with the
EndMarkedContent
method.
-
-
Save the output in a PDF document with the
SaveToFile
method.
The example below creates a tagged PDF document with some text and an image:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); // Set the measurement unit and the origin. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter); gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft); // Create a new PDF document. gdpicturePDF.NewPDF(); // Add a new A4-sized page to the document. gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4); // Set the font and the font size to be used in the PDF document. string fontResourceName = gdpicturePDF.AddTrueTypeFontU("Arial", false, false, true); gdpicturePDF.SetTextSize(18); // Load the image to be used in the PDF document. string logo = gdpicturePDF.AddJpegImageFromFile(@"C:\temp\logo.jpg"); // Set the title. gdpicturePDF.SetTitle("Company Logo"); // Set the language. gdpicturePDF.SetLanguage("en-US"); // Determine the ID of the root tag. int tagRootId = gdpicturePDF.GetTagRootID(); // Create a section tag for the document content. int tagSection = gdpicturePDF.NewTag(tagRootId, "Sect"); // Add text to the section. gdpicturePDF.BeginMarkedContentSequence(tagSection, "Span"); gdpicturePDF.DrawText(fontResourceName, 20, 60, "The company logo is"); gdpicturePDF.EndMarkedContent(); // Create a figure tag within the section and set its properties. int tagLogo = gdpicturePDF.NewTag(tagSection, "Figure"); gdpicturePDF.SetTagAlternateDescription(tagLogo, "This is the company logo."); // Add the image to the figure tag. gdpicturePDF.BeginMarkedContentSequence(tagLogo, "Figure"); gdpicturePDF.DrawImage(logo, 80, 80, 40, 40); gdpicturePDF.EndMarkedContent(); // Save the output in a PDF document. gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() ' Set the measurement unit and the origin. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter) gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Create a new PDF document. gdpicturePDF.NewPDF() ' Add a new A4-sized page to the document. gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) ' Set the font and the font size to be used in the PDF document. Dim fontResourceName As String = gdpicturePDF.AddTrueTypeFontU("Arial", False, False, True) gdpicturePDF.SetTextSize(18) ' Load the image to be used in the PDF document. Dim logo As String = gdpicturePDF.AddJpegImageFromFile("C:\temp\logo.jpg") ' Set the title. gdpicturePDF.SetTitle("Company Logo") ' Set the language. gdpicturePDF.SetLanguage("en-US") ' Determine the ID of the root tag. Dim tagRootId As Integer = gdpicturePDF.GetTagRootID() ' Create a section tag for the document content. Dim tagSection As Integer = gdpicturePDF.NewTag(tagRootId, "Sect") ' Add text to the section. gdpicturePDF.BeginMarkedContentSequence(tagSection, "Span") gdpicturePDF.DrawText(fontResourceName, 20, 60, "The company logo is") gdpicturePDF.EndMarkedContent() ' Create a figure tag within the section and set its properties. Dim tagLogo As Integer = gdpicturePDF.NewTag(tagSection, "Figure") gdpicturePDF.SetTagAlternateDescription(tagLogo, "This is the company logo.") ' Add the image to the figure tag. gdpicturePDF.BeginMarkedContentSequence(tagLogo, "Figure") gdpicturePDF.DrawImage(logo, 80, 80, 40, 40) gdpicturePDF.EndMarkedContent() ' Save the output in a PDF document. gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using