Create Accessible PDF Documents Using C#

Suresh Mohan - May 4 '22 - - Dev Community

Adding tags to the elements of a PDF document makes it accessible. When a tagged PDF document has accessibility tags, it is useful for screen readers and other assistive technologies to read and navigate the document.

With the help of the Syncfusion PDF Library, you can easily create a tagged PDF document that is compatible with Web Content Accessibility Guidelines (WCAG) 2.0 (ISO/IEC 40500:2012) and the PDF/UA (ISO 14289-1) standard.

In this blog, we will cover the following:

What is an accessibility tag?

Accessibility tags give semantic meaning to PDF elements so that assistive technology can automatically recognize them.

Accessibility tags have names similar to HTML tags. You can find the list of standard tags in the standard PDF tags documentation.

Though these tags are embedded within the PDF document, they won’t make any visual differences. These tags define the structure of the PDF elements so that they are easily recognized by a screen reader or other text-to-speech software.

Create a simple accessible PDF

A PDF document has many visual elements such as text, lists, images, and tables. These elements can be tagged using standard PDF tags. For more details on the tags that can added using Syncfusion PDF Library, refer to the supported PDF tags class reference.

Let’s create an accessible PDF.

Step 1 : Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your .NET applications from nuget.org.

Installing PDF.NET Core NuGet

Step 2 : Use the PdfStructureElement class to create and set tags to the PDF elements, and set the different types of tags using the PdfTagType enum.

Refer to the following code example to create a PDF document with a structure element.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

//Create new PDF document
using (PdfDocument doc = new PdfDocument())
{

  //Set the document title
  doc.DocumentInformation.Title = "PdfTextElement";

  //Create a new page
  PdfPage page = doc.Pages.Add();

  //Initialize the structure element with tag type paragraph
  PdfStructureElement structureElement = new PdfStructureElement(PdfTagType.Paragraph);

  //Represents the text that is the exact replacement for PdfTextElement
  structureElement.ActualText = "Simple paragraph element";

  string text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European, and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base.";

  //Initialize the PDF text element
  PdfTextElement element = new PdfTextElement(text);

  //Adding tag to the text element
  element.PdfTag = structureElement;

  //Create font for the text element
  element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12);

  element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93));

  //Draw text element with tag
  element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width, 200));

  FileStream output = new FileStream("TaggedPDF.pdf", FileMode.CreateNew);

  //Save the document
  doc.Save(output);

}
Enter fullscreen mode Exit fullscreen mode

By executing this code example, you will get a PDF document similar to the following screenshot.

Accessible PDF created using C# Note: For more information about adding different tags in the PDF document, refer to the working with a tagged PDF documentation.

Auto-tagging a new PDF document

The Syncfusion PDF Library provides an auto-tag feature to automatically tag PDF elements with the appropriate tag types.

This feature helps users to easily convert regular PDFs to accessible PDF documents.

Here is the same code used in the previous section with auto-tagging enabled.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

using (PdfDocument doc = new PdfDocument())
{

  //Enable auto-tagging
  doc.AutoTag = true;

  //Set the document title
  doc.DocumentInformation.Title = "PdfTextElement";

  //Create new page
  PdfPage page = doc.Pages.Add();

  string text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base.";

  //Initialize the PDF text element
  PdfTextElement element = new PdfTextElement(text);

  //Create font for the text element
  element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12);

  element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93));

  //Draw text element with tag
  element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width, 200));

  FileStream output = new FileStream("Auto-tagging.pdf", FileMode.CreateNew);

  //Save the document
  doc.Save(output);
}
Enter fullscreen mode Exit fullscreen mode

By executing this code example, you will get a PDF document similar to the following screenshot.

Accessible PDF created using auto tagging feature

Create a PDF conformance document with accessibility tags

PDF/A is a PDF conformance format that helps in archiving, long-term preservation, and exchange of electronic documents.

PDF/A-3a is the conformance level to create an accessible PDF document.

The following code demonstrates the creation of a PDF/A-3a document.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

//Create new PDF document
using (PdfDocument doc = new PdfDocument(PdfConformanceLevel.Pdf_A3A))
{

  //Enable auto-tagging
  doc.AutoTag = true;

  //Set the document title
  doc.DocumentInformation.Title = "PDF conformance";

  //Create new page
  PdfPage page = doc.Pages.Add();

  string text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base.";

  //Initialize the PDF text element
  PdfTextElement element = new PdfTextElement(text);

  FileStream fontStream = new FileStream("Arial.ttf", FileMode.Open, FileAccess.Read);

  PdfFont font = new PdfTrueTypeFont(fontStream, 14);

  //Create font for the text element
  element.Font = font;

  element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93));

  //Draw text element with tag
  element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width, 200));

  FileStream output = new FileStream("PDFA3a.pdf", FileMode.CreateNew);

  //Save the document
  doc.Save(output);
}
Enter fullscreen mode Exit fullscreen mode

By executing this code example, you will get a PDF document similar to the following screenshot.

Accessible PDF with PDF Conformance

Convert a Word document to a tagged PDF

Microsoft Word documents are one of the most widely used file formats to edit and exchange content. With the help of the Syncfusion Word-to-PDF converter, you can easily convert any Word document to a PDF with accessibility tags. Here’s how to do it:

Step 1 : Install the Syncfusion.Pdf.Net.Core and Syncfusion.DocIORenderer.Net.Core NuGet packages as references to your .NET applications from nuget.org.

Installing PDF.NET.Core and DocIO Renderer.NET.Core NuGetStep 2: Enable the AutoTag property in the settings to tag the PDF document while converting it.

The following code converts a Word document to a tagged PDF.

using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

//Opens the file as a stream
FileStream docStream = new FileStream(@"../../../DocToPDF.docx", FileMode.Open, FileAccess.Read);

//Loads file stream into Word document
using (WordDocument wordDocument = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic))
{

  using (DocIORenderer render = new DocIORenderer())
  {

    //Sets true to preserve document-structured tags in the converted PDF document

    render.Settings.AutoTag = true;

    //Converts Word document into PDF document

    PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);

    //Saves the PDF file
    FileStream outputStream = new FileStream("output.pdf", FileMode.CreateNew);

    pdfDocument.Save(outputStream);

    //Closes the instance of PDF document object

    pdfDocument.Close(true);
  }

}
Enter fullscreen mode Exit fullscreen mode

By executing this code example, you will get a PDF document similar to the following screenshot.

Accessible PDF created from Word document

Conclusion

In this blog post, we have walked through the steps to create a tagged PDF document, convert a regular PDF to a tagged PDF, enable auto-tagging, create PDF/A-3a documents, and convert a Word document to an accessible PDF in C# using the Syncfusion PDF Library.

Take a moment to peruse our documentation where you’ll find other options and features, all with accompanying code examples.

If you have any questions about these features, please let us know in the comments section below. You can also contact us through our support forum, support portal, or feedback portal. We are happy to assist you!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .