Create a Tagged PDF Document in Java

·

3 min read

A tagged PDF is a PDF file that contains additional metadata about the content of the elements in the document. A tag is an invisible label that can tell the assistive technology user what is on the page. This article will share how to programmatically create a tagged PDF document using Spire.PDF for Java.

Import Dependency (2 methods)

(Spire.PDF for Java is a commercial library for processing PDF files. If you want to remove the evaluation message from the generated documents, you could contact to get a 30-day trial license.)

Method 1: Download the library and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.

Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

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

Sample Code

To create a tagged Pdf file, you need first create an object of PdfTaggedContent class. Then, add an element to the root using PdfTaggedContent.getStructureTreeRoot().appendChildElement() method. The complete sample code is shown as below.

import com.spire.pdf.*;
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.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 {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

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

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

        //Create an object of PdfTaggedContent class
        PdfTaggedContent taggedContent = new PdfTaggedContent(doc);

        //Set language and title for the document
        taggedContent.setLanguage("en-US");
        taggedContent.setTitle("Create Tagged PDF in Java");

        //Set PDF/UA1 identification
        taggedContent.setPdfUA1Identification();

        //Create font and brush
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman",Font.PLAIN,14), true);
        PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));

        //Add a "document" element
        PdfStructureElement document = taggedContent.getStructureTreeRoot().appendChildElement(PdfStandardStructTypes.Document);

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

        //Add a "paragraph" element
        PdfStructureElement paragraph = document.appendChildElement(PdfStandardStructTypes.Paragraph);
        paragraph.beginMarkedContent(page);
        String paragraphText = "Tagged PDFs can mean the difference between accessing information and " +
                "being unable to do everyday digital tasks like checking your bank balance, " +
                "viewing your statement of benefits from your healthcare provider, accessing college materials, and shopping online." ;

        Rectangle2D.Float rect = new Rectangle2D.Float(0, 30, (float) page.getCanvas().getClientSize().getWidth(), (float) page.getCanvas().getClientSize().getHeight());
        page.getCanvas().drawString(paragraphText, font, brush, rect);
        paragraph.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 = {"Name;Age;Sex",
                "John;22;Male",
                "Katty;25;Female"
        };
        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(0, 120), 300f);
        table.endMarkedContent(page);

        //Add a "figure" element
        PdfStructureElement figure = document.appendChildElement(PdfStandardStructTypes.Figure);
        figure.beginMarkedContent(page);
        PdfImage image = PdfImage.fromFile("pdf.jpg");
        page.getCanvas().drawImage(image, new Point2D.Float(0, 200));
        figure.endMarkedContent(page);

        //Save the document to file
        doc.saveToFile("CreatePDFUA.pdf");
    }
}

taggedPDF.jpg