DEV Community

Alexis
Alexis

Posted on

Java Created Tagged PDF

The Tagged PDF is a PDF with structure elements and it is used to making content accessible to users who rely on assistive technology. Spire.PDF for Java supports to create tagged PDF in Java applications. This article will demonstrate how to add tags to PDF elements such as text element, image, shapes and tables from the following three parts:

Install Spire.PDF for Java

First of all, you need to add the Spire.PDF.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file by adding the following code to your project's pom.xml file.

<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.12.6</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Adding tag to Text

Spire.PDF for java offers PdfTaggedContent class to create PDF with Tagged Text. Here are the detail steps:

  • Create a PdfDocument object and add a page to it using PdfDocument.getPages.add() method.
  • Set tab order as structure using page.setTabOrder(TabOrder.Structure)method;
  • Create an object of PdfTaggedContent instance.
  • Create Structure Elements by adding a "document" element to the root of the document using PdfTaggedContent.getStructureTreeRoot().appendChildElement() method.
  • Add a "paragraph" element under the "document" element using PdfStructureElement.appendChildElement() method.
  • Add a start tag using PdfStructureElement.beginMarkedContent() method, which indicates the beginning of the heading element.
  • Draw paragraph text on the page using PdfPageBase.getCanvas.drawString() method.
  • Add an end tag using PdfStructureElement.endMarkedContent() method, which implies the paragraph element ends here.
  • Save the document to a PDF file using PdfDocument.saveToFile() method.
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 java.awt.*;

public class createTaggedPDF {

    public static void main(String[] args) throws Exception {


        //Create a pdf document
        PdfDocument doc = new PdfDocument();

        //Add page
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, new PdfMargins(20));
        //Set tab order
        page.setTabOrder(TabOrder.Structure);

        //Create PdfTaggedContent instance
        PdfTaggedContent taggedContent = new PdfTaggedContent(doc);

        //Set Title and Language for Document
        taggedContent.setLanguage("en-US");
        taggedContent.setTitle("Tagged PDF");

        //Set font
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman",Font.PLAIN,12), true);
        PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.black));

        //Create Structure Elements
        PdfStructureElement article = taggedContent.getStructureTreeRoot().appendChildElement(PdfStandardStructTypes.Document);
        PdfStructureElement paragraph1 = article.appendChildElement(PdfStandardStructTypes.Paragraph);
        PdfStructureElement span1 = paragraph1.appendChildElement(PdfStandardStructTypes.Span);
        article.setTitle("Tag for Text");

        //Add tagged paragraph
        span1.beginMarkedContent(doc.getPages().get(0));
        PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Justify);
        page.getCanvas().drawString("First paragraph of the PDF document", font, brush, new Rectangle(40, 0, 480, 80), format);
        paragraph1.setTitle("The first Tag for Text");
        span1.endMarkedContent(doc.getPages().get(0));

        //Add another tagged paragraph
        PdfStructureElement paragraph2 = article.appendChildElement(PdfStandardStructTypes.Paragraph);
        paragraph2.beginMarkedContent(doc.getPages().get(0));
        page.getCanvas().drawString("Second paragraph of the PDF document",font, brush, new Rectangle(40, 80, 480, 60), format);
        paragraph2.endMarkedContent(doc.getPages().get(0));
        paragraph2.setTitle("The second Tag for Text");

        //Save to file
        String result = "CreateTaggedPDF.pdf";
        doc.saveToFile(result);
        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Tagged text in PDF

Adding tag to Image:

You can add tag to image in the PDF document by using the PdfTaggedContent instance offered by Spire.PDF for java.

  • Create a PdfDocument object and add a page to it using PdfDocument.getPages.add() method.
  • Set tab order as structure using page.setTabOrder(TabOrder.Structure)method;
  • Create an object of PdfTaggedContent instance.
  • Create Structure Elements by adding a "document" element to the root of the document using PdfTaggedContent.getStructureTreeRoot().appendChildElement() method.
  • Add a “figure" element under the "document" element using PdfStructureElement.appendChildElement() method.
  • Add a start tag using PdfStructureElement.beginMarkedContent() method, which indicates the beginning of the heading element.
  • Draw an image on the page using PdfPageBase.getCanvas.drawImage() method.
  • Add an end tag using PdfStructureElement.endMarkedContent() method, which implies the image element ends here.
  • Save the document to a PDF file using PdfDocument.saveToFile() method.
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 java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;


public class createTaggedPDF {

    public static void main(String[] args) throws Exception {

        //Create a pdf document
        PdfDocument doc = new PdfDocument();

        //Add page
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, new PdfMargins(20));
        //Set tab order
        page.setTabOrder(TabOrder.Structure);

        //Create PdfTaggedContent instance
        PdfTaggedContent taggedContent = new PdfTaggedContent(doc);

        //Set Title and Language for Document
        taggedContent.setLanguage("en-US");
        taggedContent.setTitle("Tagged PDF");

        //Create Structure Elements
        PdfStructureElement article = taggedContent.getStructureTreeRoot().appendChildElement(PdfStandardStructTypes.Document);
        PdfStructureElement figure = article.appendChildElement(PdfStandardStructTypes.Figure);
        article.setTitle("Tag for Image");

        //Add tagged image
        figure.setAlt("Replacement text");
        figure.beginMarkedContent(doc.getPages().get(0), null);
        PdfImage image = PdfImage.fromFile("1.png");
        Dimension2D dimension2D = new Dimension();
        dimension2D.setSize( 200,200);
        page.getCanvas().drawImage(image, new Point2D.Float(40, 80),dimension2D);
        figure.endMarkedContent(doc.getPages().get(0));
        figure.setTitle("Adding tag to Image");

        //Save to file
        String result = "CreateTaggedImage.pdf";
        doc.saveToFile(result);
        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Tagged image in PDF

Adding tag to Table

Spire.PDF for java also supports to adding tag to the table in the PDF file. Here are the detail steps:

  • Create a PdfDocument object and add a page to it using PdfDocument.getPages.add() method.
  • Set tab order as structure using page.setTabOrder(TabOrder.Structure)method;
  • Create an object of PdfTaggedContent instance.
  • Create Structure Elements by adding a "document" element to the root of the document using PdfTaggedContent.getStructureTreeRoot().appendChildElement() method.
  • Add a "table" element under the "document" element using PdfStructureElement.appendChildElement() method.
  • Add a start tag using PdfStructureElement.beginMarkedContent() method, which indicates the beginning of the heading element.
  • Draw a table to the PDF page using PdfTable.draw(page, new Point2D.Float(x, y)) method.
  • Add an end tag using PdfStructureElement.endMarkedContent() method, which implies the table element ends here.
  • Save the document to a PDF file using PdfDocument.saveToFile() method.
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.PdfColumn;
import com.spire.pdf.tables.PdfHeaderSource;
import com.spire.pdf.tables.PdfTable;

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

public class createTaggedPDF {

    public static void main(String[] args) throws Exception {

        //Create a pdf document
        PdfDocument doc = new PdfDocument();

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

        //Set tab order
        doc.getPages().get(0).setTabOrder(TabOrder.Structure);

        //Create PdfTaggedContent class
        PdfTaggedContent taggedContent = new PdfTaggedContent(doc);

        //Set Title and Language for Document
        taggedContent.setLanguage("en-US");
        taggedContent.setTitle("Tagged PDF");

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

        //Create Structure Elements
        PdfStructureElement article = taggedContent.getStructureTreeRoot().appendChildElement(PdfStandardStructTypes.Document);
        PdfStructureElement figure = article.appendChildElement(PdfStandardStructTypes.Table);
        figure.setTitle("Adding tag to table");

        //Add tag to a table
        figure.beginMarkedContent(doc.getPages().get(0), null);

        //Add a table to the PDF page and set the data for it

        PdfTable table = new PdfTable();
        table.getStyle().setBorderPen(new PdfPen(brush, 0.5f));
        table.getStyle().getHeaderStyle().setStringFormat(new PdfStringFormat(PdfTextAlignment.Center));
        table.getStyle().setHeaderSource(PdfHeaderSource.Rows);
        table.getStyle().setHeaderRowCount(1);
        table.getStyle().setShowHeader(true);
        table.getStyle().setCellPadding(2);
        table.getStyle().setHeaderSource(PdfHeaderSource.Rows);
        table.getStyle().setHeaderRowCount(1);
        table.getStyle().setShowHeader(true);

        String[] data = {"Name;Age;Sex;Note",
                "John;23;Male;",
                "Katty;21;Female;",
                "Coco;25;Female;",
                "Kyle;27;Male; "
        };
        String[][] dataSource = new String[data.length][];
        for (int i = 0; i < data.length; i++) {
            dataSource[i] = data[i].split("[;]", -1);
        }

        table.setDataSource(dataSource);
        for(int i = 0; i < table.getColumns().getCount();i++)
        {
            PdfColumn column= table.getColumns().get(i);
            column.setStringFormat(new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));
        }

        //Add the table
        table.draw(page, new Point2D.Float(0, 50));

        figure.endMarkedContent(page);

        //Save to file
        String result = "CreateTaggedTable.pdf";
        doc.saveToFile(result);
        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Tagged table in PDF

Conclusion

In this article, we have demonstrated how to created tagged PDF document using Java. With Spire.PDF for Java, we could also add tag to hyperlink in PDF; add tag annotation to PDF and extract the tagged content from the PDF. You can check the PDF forum for more features to operate the PDF files.

Top comments (0)