A tagged PDF file uses tags to define the logical structure of the content in the document. This type of PDF improves the reading experience using assistive technologies and screen readers. In the previous post, you have seen how to create PDF files programmatically in Java applications. In this article, you will learn how to create tagged PDF files programmatically in Java.
- Java 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
Java API to Create Tagged PDF Files
To create the tagged PDF files, we will use Aspose.PDF for Java. It is one of the popular APIs for PDF generation and manipulation. Using the API, you can seamlessly create, process, and convert PDF files of simple and complex layouts. You can download the API or install it using the following Maven configurations.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.4</version>
</dependency>
Create a Tagged PDF File in Java
To create structure elements in a tagged PDF, Aspose.PDF for Java provides the ITaggedContent interface. So let’s see how to use this interface to create a tagged PDF file in Java.
- Create a new PDF 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.getRootElement().appendChild() method.
- Finally, save the PDF file using Document.save(String) method.
The following code sample shows how to create a tagged PDF in Java.
The following is the output of the above code sample.
Create a Tagged PDF with Nested Elements in Java
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.getRootElement().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 Java.
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 Java allows you to set font, font size, font style, and foreground color using StructureTextState class. The following code sample shows how to apply text styling in a tagged PDF in Java.
Illustrating Structure Elements in a Tagged PDF in Java
To illustrate the structure elements, Aspose.PDF for Java 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 Java without evaluation limitations.
Conclusion
In this article, you have learned how to create tagged PDF files in Java. Furthermore, you have seen how to create nested elements, apply styling to text, and illustrate structure elements in a tagged PDF programmatically. In addition, you can explore more about Java PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.