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:

  1. Create a GdPicturePDF object.

  2. Set the measurement unit with the SetMeasurementUnit method. This method takes a member of the PdfMeasurementUnit enumeration as its parameter. For example, to set the unit of measurement to millimeters, call gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter).

  3. Set the origin of the coordinate system with the SetOrigin method. This method takes a member of the PdfOrigin enumeration as its parameter. For example, to set the origin to the top-left corner, call gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft).

  4. Create a new PDF document with the NewPDF method. Optionally, specify the conformance level with a member of the PdfConformance enumeration.

  5. Add a new A4-sized page to the document with the NewPage method. This method takes a member of the PdfPageSizes enumeration as its parameter.

  6. Set the font type to be used in the PDF document with the AddTrueTypeFontU method, and store the font resource name in a variable.

  7. Set the font size in points with the SetTextSize method. For more information, see Text Settings in PDFs.

  8. Load the image to be used in the PDF document with the AddJpegImageFromFile method.

  9. 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.

  10. Set the language with the SetLanguage method.

  11. Determine the ID of the root tag with the GetTagRootID method.

  12. 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 is Sect.

  13. 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 is Span.

    • 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.

  14. 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 is Figure.

  15. Set an alternate description to the image with the SetTagAlternateDescription method.

  16. 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 is Figure.

    • 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.

  17. 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
Used Methods

Related Topics