C#/VB.NET: Create a Tagged PDF Document

2022-10-20 01:27:14 Written by  support iceblue
Rate this item
(21 votes)

A tagged PDF (also known as PDF/UA) is a type of PDF that includes an underlying tag tree, similar to HTML, that defines the structure of the document. These tags can help screen readers to navigate throughout the document without any loss of information. This article introduces how to create a tagged PDF from scratch in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Create a Tagged PDF with Rich Elements

To add structure elements in a tagged PDF document, we must first create an object of PdfTaggedContent class. Then, add an element to the root using PdfTaggedContent.StructureTreeRoot.AppendChildElement() method. The following are the detailed steps to add a "heading" element to a tagged PDF using Spire.PDF for .NET.

  • Create a PdfDocument object and add a page to it using PdfDocument.Pages.Add() method.
  • Create an object of PdfTaggedContent class.
  • Make the document compliance to PDF/UA identification using PdfTaggedContent.SetPdfUA1Identification() method.
  • Add a "document" element to the root of the document using PdfTaggedContent.StructureTreeRoot.AppendChildElement() method.
  • Add a "heading" element under the "document" element using PdfStructureElement.AppendChildElement() method.
  • Add a start tag using PdfStructureElement.BeginMarkedContent() method, which indicates the beginning of the heading element.
  • Draw heading text on the page using PdfPageBase.Canvas.DrawString() method.
  • Add an end tag using PdfStructureElement.BeginMarkedContent() method, which implies the heading element ends here.
  • Save the document to a PDF file using PdfDocument.SaveToFile() method.

The following code snippet provides an example on how to create various elements including document, heading, paragraph, figure and table in a tagged PDF document in C# and VB.NET.

  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interchange.TaggedPdf;
using Spire.Pdf.Tables;
using System.Data;
using System.Drawing;

namespace CreatePDFUA
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Add a page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(20));          

            //Set tab order
            page.SetTabOrder(TabOrder.Structure);

            //Create an object of PdfTaggedContent class
            PdfTaggedContent taggedContent = new PdfTaggedContent(doc);

            //Set language and title for the document
            taggedContent.SetLanguage("en-US");
            taggedContent.SetTitle("test");

            //Set PDF/UA1 identification
            taggedContent.SetPdfUA1Identification();

            //Create font and brush
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 14), true);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);

            //Add a "document" element 
            PdfStructureElement document = taggedContent.StructureTreeRoot.AppendChildElement(PdfStandardStructTypes.Document);

            //Add a "heading" element
            PdfStructureElement heading1 = document.AppendChildElement(PdfStandardStructTypes.HeadingLevel1);
            heading1.BeginMarkedContent(page);      
            string headingText = "What Is a Tagged PDF?";
            page.Canvas.DrawString(headingText, font, brush, new PointF(0, 0));
            heading1.EndMarkedContent(page);

            //Add a "paragraph" element 
            PdfStructureElement paragraph = document.AppendChildElement(PdfStandardStructTypes.Paragraph);
            paragraph.BeginMarkedContent(page);
            string paragraphText = "“Tagged PDF” doesn’t seem like a life-changing term. But for some, it is. For people who are " +
                "blind or have low vision and use assistive technology (such as screen readers and connected Braille displays) to " +
                "access information, an untagged PDF means they are missing out on information contained in the document because assistive " +
                "technology cannot “read” untagged PDFs.  Digital accessibility has opened up so many avenues to information that were once " +
                "closed to people with visual disabilities, but PDFs often get left out of the equation.";
            RectangleF rect = new RectangleF(0, 30, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height);
            page.Canvas.DrawString(paragraphText, font, brush, rect);
            paragraph.EndMarkedContent(page);

            //Add a "figure" element to 
            PdfStructureElement figure = document.AppendChildElement(PdfStandardStructTypes.Figure);
            figure.BeginMarkedContent(page);
            PdfImage image = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\pdfua.png");
            page.Canvas.DrawImage(image, new PointF(0, 150));
            figure.EndMarkedContent(page);

            //Add a "table" element
            PdfStructureElement table = document.AppendChildElement(PdfStandardStructTypes.Table);
            table.BeginMarkedContent(page);
            PdfTable pdfTable = new PdfTable();
            pdfTable.Style.DefaultStyle.Font = font;
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("Age");
            dataTable.Columns.Add("Sex");
            dataTable.Rows.Add(new string[] { "John", "22", "Male" });
            dataTable.Rows.Add(new string[] { "Katty", "25", "Female" });
            pdfTable.DataSource = dataTable;
            pdfTable.Style.ShowHeader = true;
            pdfTable.Draw(page.Canvas, new PointF(0, 280), 300f);
            table.EndMarkedContent(page);

            //Save the document to file
            doc.SaveToFile("CreatePDFUA.pdf");
        }
    }
}

C#/VB.NET: Create a Tagged PDF Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 20 October 2022 01:32