A tagged PDF file is a type of PDF that uses tags to define the logical structure of the content in the document. It is used to enhance the reading experience for those who use assistive technologies and screen readers. In a previous post, we covered creating PDF files within a .NET application. In this article, we will demonstrate how to create tagged PDF files programmatically in C# .NET.
- C# Library to Create Tagged PDF Files
- Create a Tagged PDF File
- Create a Tagged PDF with Nested Elements
- Styling Text Structure in a Tagged PDF
- Illustrating Structure Elements in a Tagged PDF
C# Library to Create Tagged PDF - Free Download
To create tagged PDF files, we will use Aspose.PDF for .NET. It is a robust library for PDF generation and manipulation in .NET applications. Using the library, you can seamlessly create, process, and convert PDF files of simple and complex layouts. You can download the library’s binaries or install it using NuGet.
PM> Install-Package Aspose.PDF
Create a Tagged PDF in C#
To create structure elements in a tagged PDF, Aspose.PDF for .NET provides the ITaggedContent interface. So let’s see how to use this interface to create a tagged PDF file in C#.
- Create a new PDF or load an existing one using the Document class.
- Get the reference of the TaggedContent of the document into an ITaggedContent object.
- Set title, header, and language and add elements to the PDF using the ITaggedContent object.
- Create a new ParagraphElement using ITaggedContent.CreateParagraphElement() method and set its text.
- Add the paragraph to the document using ITaggedContent.RootElement.AppendChild() method.
- Finally, save the PDF file using Document.Save(String) method.
The following code sample shows how to create a tagged PDF in C#.
The following is the output of the above code sample.
C# Create a Tagged PDF with Nested Elements
In the previous example, we created a simple tagged PDF that contains a paragraph. Let’s now have a look at how to add nested elements in a tagged PDF. The following are the steps to perform this operation.
- Create a new PDF or load an existing one using the Document class.
- Get the reference of the TaggedContent of the document into an ITaggedContent object.
- Set title, header, and language and add elements to the PDF using the ITaggedContent object.
- Create a new ParagraphElement using ITaggedContent.CreateParagraphElement() method and set its text.
- Use SpanElement class to add the nested elements.
- Add nested element to paragraph using ParagraphElement.AppendChild() method.
- Add the paragraph to the document using ITaggedContent.RootElement.AppendChild() method.
- Finally, save the PDF file using Document.Save(String) method.
The following code sample shows how to add nested elements in a tagged PDF in C#.
The following screenshot shows the tagged PDF with nested elements.
Styling Text Structure in a Tagged PDF in C#
You can also apply styling to the text in a tagged PDF by setting font style, family, size, etc. For this, Aspose.PDF for .NET provides Font, FontSize, FontStyle and ForegroundColor properties of StructureTextState class. The following code sample shows how to apply text styling in a tagged PDF in C#.
C# Illustrating Structure Elements in a Tagged PDF
To illustrate the structure elements, Aspose.PDF for .NET provides IllustrationElement class. The following code sample shows how to use this class to illustrate the structural elements in a tagged PDF.
Free PDF Library to Create Tagged PDF
You can get a free temporary license and create tagged PDF files without evaluation limitations.
Explore C# PDF Library
Besides, you can explore more about the C# PDF library using the documentation. In case you would have any questions or queries, you can contact us via our forum.
Conclusion
In this article, you have learned how to create tagged PDF files in C#. Furthermore, you have seen how to create nested elements, apply styling to text, and illustrate structure elements in a tagged PDF programmatically.