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.
- .NET API 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# .NET API to Create Tagged PDF Files
Aspose.PDF for .NET is a robust API for PDF generation and manipulation within the .NET applications. Using the API, you can seamlessly create, process, and convert PDF files of simple and complex layouts. You can download the API’s binaries or install it using NuGet.
PM> Install-Package Aspose.PDF
Create a Tagged PDF File 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.
Create a Tagged PDF with Nested Elements in C#
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
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#.
Illustrating Structure Elements in a Tagged PDF in C#
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 structure elements in a tagged PDF.
Get a Free License
You can get a free temporary license in order to use Aspose.PDF for .NET without evaluation limitations.
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. Besides, you can explore more about .NET PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.