Sitemap

Create Tagged PDF in Java

4 min readJan 19, 2023

--

A tagged PDF is a PDF document in which each content element is labeled with a tag. These tags are invisible on the document pages, but they can help assistive technologies understand the structure of the document and thereby enabling people with disabilities to access the information in the document. A tagged PDF can also provide a high-quality reading experience to users using smart mobile devices because it can re-flow to adapt its presentation to different screen sizes. In this article, I will demonstrate how to create tagged PDF documents in Java.

Add Dependencies

To create tagged PDFs, this article uses Spire.PDF for Java. If you are using maven, you can install the jar of Spire.PDF for Java into your project by adding the following code to your project’s pom.xml file.

<repositories>   
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.pdf </artifactId>
<version>9.1.4</version>
</dependency>
</dependencies>

If you are not using maven, you can download Spire.PDF for Java from this website, extract the package and then import the Spire.Pdf.jar under the lib folder into your project as a dependency.

Create Tagged PDF in Java

Spire.PDF provides the PdfTaggedContent class to generate tagged PDF documents. You can include various structural elements in a tagged PDF document, including heading, paragraph, figure, table, link, list, and many more.

Get Alice Yang’s stories in your inbox

Join Medium for free to get updates from this writer.

The following are the main steps to create a tagged PDF with various structural elements using Java:

  • Initialize an instance of the PdfDocument class and add a page to it using PdfDocument.getPages().add() method.
  • Set the tab order of the page using PdfPageBase.setTabOrder(TabOrder) method.
  • Initialize an instance of the PdfTaggedContent class and pass the PdfDocument instance to the class constructor as a parameter.
  • Set the language and title of the document.
  • Add a document element which is the root element of a structure tree to the document using PdfTaggedContent.getStructureTreeRoot().appendChildElement() method.
  • Add a heading/paragraph/list/figure/table/ link element under the document element using PdfStructureElement.appendChildElement() method.
  • Add a start tag to indicate the beginning of the element using PdfStructureElement.beginMarkedContent() method.
  • Specify the element content and draw it onto the page.
  • Add an end tag to indicate the ending of the element using PdfStructureElement.endMarkedContent() method.
  • Save the result document to a PDF file using PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.TabOrder;
import com.spire.pdf.annotations.PdfTextWebLink;
import com.spire.pdf.graphics.*;
import com.spire.pdf.interchange.taggedpdf.PdfStandardStructTypes;
import com.spire.pdf.interchange.taggedpdf.PdfStructureElement;
import com.spire.pdf.interchange.taggedpdf.PdfTaggedContent;
import com.spire.pdf.lists.PdfMarker;
import com.spire.pdf.lists.PdfUnorderedList;
import com.spire.pdf.lists.PdfUnorderedMarkerStyle;
import com.spire.pdf.tables.PdfTable;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class CreateTaggedPDF {
public static void main(String []args) throws Exception {
//Initialize an instance of the PdfDocument class
PdfDocument doc = new PdfDocument();

//Add a page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, new PdfMargins(20));

//Set tab order
page.setTabOrder(TabOrder.Structure);

//Initialize an instance of the PdfTaggedContent class
PdfTaggedContent taggedContent = new PdfTaggedContent(doc);

//Set language and title
taggedContent.setLanguage("en-US");
taggedContent.setTitle("Tagged PDF");

//Set PDF/UA-1 compliance
taggedContent.setPdfUA1Identification();

//Create font and brush
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", Font.PLAIN,14), true);
PdfBrush brush = PdfBrushes.getBlack();

//Add a document element which is the root element of a structure tree
PdfStructureElement document = taggedContent.getStructureTreeRoot().appendChildElement(PdfStandardStructTypes.Document);

//Add a heading element
PdfStructureElement heading1 = document.appendChildElement(PdfStandardStructTypes.HeadingLevel1);
heading1.beginMarkedContent(page);
String headingText = "What Is Java?";
page.getCanvas().drawString(headingText, font, brush, 10, 0);
heading1.endMarkedContent(page);

//Add a paragraph element
PdfStructureElement paragraph = document.appendChildElement(PdfStandardStructTypes.Paragraph);
paragraph.beginMarkedContent(page);
String paragraphText = "Java is a computer programming language that was first released in 1995. It is owned by Oracle, and more than 3 billion devices run Java. It can be used for:";
Rectangle2D rect = new Rectangle2D.Float(10, 30, (float)page.getCanvas().getClientSize().getWidth(), (float)page.getCanvas().getClientSize().getHeight());
page.getCanvas().drawString(paragraphText, font, brush, rect);
paragraph.endMarkedContent(page);

//Add a list element
PdfStructureElement listElement = document.appendChildElement(PdfStandardStructTypes.List);
listElement.beginMarkedContent(page);
String listContent = "Mobile applications (specially Android apps)\n"
+ "Desktop applications\n"
+ "Web applications\n"
+ "Web servers and application servers\n"
+ "Database connection\n"
+ "Games\n"
+ "And many more!";
PdfMarker marker = new PdfMarker(PdfUnorderedMarkerStyle.Asterisk);
PdfTrueTypeFont markerFont = new PdfTrueTypeFont(new Font("Times New Roman", Font.PLAIN,6), true);
marker.setFont(markerFont);
PdfUnorderedList list = new PdfUnorderedList(listContent);
list.setFont(font);
list.setIndent(2);
list.setTextIndent(4);
list.setBrush(brush);
list.setMarker(marker);
list.draw(page, 10, 70);
listElement.endMarkedContent(page);

//Add a figure element
PdfStructureElement figure = document.appendChildElement(PdfStandardStructTypes.Figure);
figure.beginMarkedContent(page);
PdfImage image = PdfImage.fromFile("JavaLogo.png");
page.getCanvas().drawImage(image, 10, 190);
figure.endMarkedContent(page);

//Add a table element
PdfStructureElement table = document.appendChildElement(PdfStandardStructTypes.Table);
table.beginMarkedContent(page);
PdfTable pdfTable = new PdfTable();
pdfTable.getStyle().getDefaultStyle().setFont(font);
String[] data = {"Year; Product; Sales",
"2020; Components; $20,000",
"2021; Bikes; $6,300"};
String[][] dataSource = new String[data.length][];
for (int i = 0; i < data.length; i++) {
dataSource[i] = data[i].split("[;]", -1);
}
pdfTable.setDataSource(dataSource);
pdfTable.getStyle().setShowHeader(true);
pdfTable.draw(page.getCanvas(), new Point2D.Float(10, 280), 270f);
table.endMarkedContent(page);

//Add a link element
PdfStructureElement link = document.appendChildElement(PdfStandardStructTypes.Link);
link.beginMarkedContent(page);
PdfTextWebLink link2 = new PdfTextWebLink();
link2.setText("www.google.com");
link2.setUrl("https://www.google.com");
link2.setFont(font);
link2.setBrush(brush);
link2.drawTextWebLink(page.getCanvas(), new Point2D.Float(10, 340));
link.endMarkedContent(page);

//Save the result document
doc.saveToFile("CreateTaggedPdf.pdf");
}
}
Press enter or click to view image in full size
Create Tagged PDF in Java
Create Tagged PDF in Java

--

--

Alice Yang
Alice Yang

Written by Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.