Create Tagged PDF Files in Java

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

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.

The following code sample shows how to create a tagged PDF in Java.

// Create a new document
Document document = new Document();
// Get Content for work with Tagged PDF
ITaggedContent taggedContent = document.getTaggedContent();
StructureElement rootElement = taggedContent.getRootElement();
// Set title and language for document
taggedContent.setTitle("Tagged Pdf Document");
taggedContent.setLanguage("en-US");
// Add header
HeaderElement mainHeader = taggedContent.createHeaderElement();
mainHeader.setText("Main Header");
// Add paragraph
ParagraphElement paragraphElement = taggedContent.createParagraphElement();
paragraphElement.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. " +
"Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet" +
"nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus." +
"Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat" +
"sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper" +
"pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus" +
"ac iaculis eget, tempus et magna. Sed non consectetur elit. Sed vulputate, quam sed lacinia luctus," +
"ipsum nibh fringilla purus, vitae posuere risus odio id massa. Cras sed venenatis lacus.");
rootElement.appendChild(mainHeader);
rootElement.appendChild(paragraphElement);
// Save tagged PDF
document.save("tagged-pdf.pdf");

The following is the output of the above code sample.

Create a Tagged PDF File in Java

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.

The following code sample shows how to add nested elements in a tagged PDF in Java.

// Create a new document
Document document = new Document();
// Get Content for work with Tagged PDF
ITaggedContent taggedContent = document.getTaggedContent();
StructureElement rootElement = taggedContent.getRootElement();
// Set title and language for document
taggedContent.setTitle("Tagged Pdf Document");
taggedContent.setLanguage("en-US");
// Add header
HeaderElement mainHeader = taggedContent.createHeaderElement();
mainHeader.setText("Main Header");
// Create paragraph
ParagraphElement paragraphWithQuotes = taggedContent.createParagraphElement();
paragraphWithQuotes.getStructureTextState().setFont(FontRepository.findFont("Calibri"));
paragraphWithQuotes.getStructureTextState().setMarginInfo(new MarginInfo(10, 5, 10, 5));
// Add span element
SpanElement spanElement1 = taggedContent.createSpanElement();
spanElement1.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus ac iaculis eget, tempus et magna. Sed non consectetur elit. ");
QuoteElement quoteElement = taggedContent.createQuoteElement();
quoteElement.setText("Sed vulputate, quam sed lacinia luctus, ipsum nibh fringilla purus, vitae posuere risus odio id massa.");
quoteElement.getStructureTextState().setFontStyle(FontStyles.Bold | FontStyles.Italic);
SpanElement spanElement2 = taggedContent.createSpanElement();
spanElement2.setText(" Sed non consectetur elit.");
// Append to paragraph
paragraphWithQuotes.appendChild(spanElement1);
paragraphWithQuotes.appendChild(quoteElement);
paragraphWithQuotes.appendChild(spanElement2);
rootElement.appendChild(mainHeader);
rootElement.appendChild(paragraphWithQuotes);
// Save tagged PDF
document.save("tagged-pdf.pdf");

The following screenshot shows the tagged PDF with nested elements.

Create a Tagged PDF with Nested Elements in Java

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.

// Create a new document
Document document = new Document();
// Get Content for work with Tagged PDF
ITaggedContent taggedContent = document.getTaggedContent();
StructureElement rootElement = taggedContent.getRootElement();
// Set title and language for document
taggedContent.setTitle("Tagged Pdf Document");
taggedContent.setLanguage("en-US");
// Add header
HeaderElement mainHeader = taggedContent.createHeaderElement();
mainHeader.setText("Main Header");
// Create paragraph
ParagraphElement paragraphWithQuotes = taggedContent.createParagraphElement();
taggedContent.getRootElement().appendChild(paragraphWithQuotes);
// Set styling
paragraphWithQuotes.getStructureTextState().setFontSize(18F);
paragraphWithQuotes.getStructureTextState().setForegroundColor(Color.getRed());
paragraphWithQuotes.getStructureTextState().setFontStyle(FontStyles.Italic);
// Add text
paragraphWithQuotes.setText("Red italic text.");
// Save tagged PDF
document.save("tagged-pdf.pdf");

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.

// Create a new document
Document document = new Document();
// Get Content for work with Tagged PDF
ITaggedContent taggedContent = document.getTaggedContent();
StructureElement rootElement = taggedContent.getRootElement();
// Set title and language for document
taggedContent.setTitle("Tagged Pdf Document");
taggedContent.setLanguage("en-US");
// Create illustrationElement
IllustrationElement figure1 = taggedContent.createFigureElement();
taggedContent.getRootElement().appendChild(figure1);
figure1.setAlternativeText( "Figure One");
figure1.setTitle("Image 1");
figure1.setTag("Fig1");
figure1.setImage("aspose_pdf.png");
// Save tagged PDF
document.save("tagged-pdf.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.

See Also