Create Tagged PDF in Java
Creating a tagged PDF means adding structure elements that make the document easier to validate against PDF/UA accessibility requirements and easier for assistive technologies to interpret.
Create a simple tagged PDF document
Use this example when you need a minimal tagged PDF with a heading and paragraph in the logical structure tree.
- Create a new PDF Document and get its ITaggedContent.
- Set the document title and language, then create the required header and paragraph elements.
- Append the structure elements to the root element and save the document.
public static void createTaggedPdfDocumentSimple(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
StructureElement rootElement = taggedContent.getRootElement();
taggedContent.setTitle("Tagged Pdf Document");
taggedContent.setLanguage("en-US");
HeaderElement mainHeader = taggedContent.createHeaderElement();
mainHeader.setText("Main Header");
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.");
rootElement.appendChild(mainHeader, true);
rootElement.appendChild(paragraphElement, true);
document.save(outputFile.toString());
}
}
Create an advanced tagged PDF document
This example builds a richer structure by mixing headings, paragraphs, spans, quotes, and explicit layout settings.
- Create a new PDF Document and initialize tagged content metadata.
- Build the heading and paragraph structure, then add spans and a quote element inside the paragraph.
- Adjust paragraph position, append the elements to the root structure, and save the document.
public static void createTaggedPdfDocumentAdv(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
StructureElement rootElement = taggedContent.getRootElement();
taggedContent.setTitle("Tagged Pdf Document");
taggedContent.setLanguage("en-US");
HeaderElement header1 = taggedContent.createHeaderElement(1);
header1.setText("Header Level 1");
ParagraphElement paragraphWithQuotes = taggedContent.createParagraphElement();
paragraphWithQuotes.getStructureTextState().setFont(FontRepository.findFont("Arial"));
PositionSettings positionSettings = new PositionSettings();
positionSettings.setMargin(new MarginInfo(10, 5, 10, 5));
paragraphWithQuotes.adjustPosition(positionSettings);
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. ");
QuoteElement quoteElement = taggedContent.createQuoteElement();
quoteElement.setText("Sed vulputate, quam sed lacinia luctus, ipsum nibh fringilla purus.");
quoteElement.getStructureTextState().setFontStyle(Nullable.of(FontStyles.Bold | FontStyles.Italic));
SpanElement spanElement2 = taggedContent.createSpanElement();
spanElement2.setText(" Sed non consectetur elit.");
paragraphWithQuotes.appendChild(spanElement1, true);
paragraphWithQuotes.appendChild(quoteElement, true);
paragraphWithQuotes.appendChild(spanElement2, true);
rootElement.appendChild(header1, true);
rootElement.appendChild(paragraphWithQuotes, true);
document.save(outputFile.toString());
}
}
Add text style to tagged content
Use this example when tagged paragraph content should carry explicit font, color, and style information.
- Create a new tagged PDF Document.
- Create a paragraph element and configure its structure text state.
- Set the paragraph text and save the document.
public static void addStyle(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
taggedContent.setTitle("Tagged Pdf Document");
taggedContent.setLanguage("en-US");
ParagraphElement paragraphElement = taggedContent.createParagraphElement();
taggedContent.getRootElement().appendChild(paragraphElement, true);
paragraphElement.getStructureTextState().setFontSize(Nullable.of(18.0f));
paragraphElement.getStructureTextState().setForegroundColor(Color.getRed());
paragraphElement.getStructureTextState().setFontStyle(Nullable.of(FontStyles.Italic));
paragraphElement.setText("Red italic text.");
document.save(outputFile.toString());
}
}
Add figure structure elements
This example shows how to create a tagged figure with alternative text, title, custom tag, image content, and positioning.
- Create a new tagged PDF Document.
- Create a FigureElement, set its accessible metadata, and assign the image.
- Adjust the figure position and save the document.
public static void illustrateStructureElements(Path imageFile, Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
taggedContent.setTitle("Tagged Pdf Document");
taggedContent.setLanguage("en-US");
FigureElement figure1 = taggedContent.createFigureElement();
taggedContent.getRootElement().appendChild(figure1, true);
figure1.setAlternativeText("Figure One");
figure1.setTitle("Image 1");
figure1.setTag("Fig1");
figure1.setImage(imageFile.toString(), 300);
PositionSettings positionSettings = new PositionSettings();
MarginInfo marginInfo = new MarginInfo();
marginInfo.setLeft(50);
marginInfo.setTop(20);
positionSettings.setMargin(marginInfo);
figure1.adjustPosition(positionSettings);
document.save(outputFile.toString());
}
}
Validate a tagged PDF for PDF/UA
Use this example when you need to check whether a tagged PDF satisfies PDF/UA validation rules.
- Open the source PDF Document.
- Run validation against PdfFormat.
PDF_UA_1. - Write the validation log and print the validation result.
public static void validateTaggedPdf(Path inputFile, Path logFile) {
try (Document document = new Document(inputFile.toString())) {
boolean isValid = document.validate(logFile.toString(), PdfFormat.PDF_UA_1);
System.out.println("Is Valid: " + isValid);
}
}
Adjust structure element position
This example applies explicit margin and alignment settings to a tagged paragraph.
- Create a new tagged PDF Document.
- Add a paragraph structure element and prepare PositionSettings.
- Apply the position settings to the paragraph and save the document.
public static void adjustPosition(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
taggedContent.setTitle("Tagged Pdf Document");
taggedContent.setLanguage("en-US");
ParagraphElement paragraph = taggedContent.createParagraphElement();
taggedContent.getRootElement().appendChild(paragraph, true);
paragraph.setText("Text.");
PositionSettings positionSettings = new PositionSettings();
MarginInfo marginInfo = new MarginInfo();
marginInfo.setLeft(300);
marginInfo.setTop(20);
marginInfo.setRight(0);
marginInfo.setBottom(0);
positionSettings.setMargin(marginInfo);
positionSettings.setHorizontalAlignment(HorizontalAlignment.None);
positionSettings.setVerticalAlignment(VerticalAlignment.None);
positionSettings.setFirstParagraphInColumn(false);
positionSettings.setKeptWithNext(false);
positionSettings.setInNewPage(false);
positionSettings.setInLineParagraph(false);
paragraph.adjustPosition(positionSettings);
document.save(outputFile.toString());
}
}
Convert an existing PDF to PDF/UA with automatic tagging
Use this approach when an existing PDF should be converted to PDF/UA and tagged automatically during conversion.
- Open the source PDF Document.
- Create PdfFormatConversionOptions and enable automatic tagging.
- Run the conversion and save the output document.
public static void convertToPdfUaWithAutomaticTagging(Path inputFile, Path outputFile, Path logFile) {
try (Document document = new Document(inputFile.toString())) {
PdfFormatConversionOptions options = new PdfFormatConversionOptions(
logFile.toString(), PdfFormat.PDF_UA_1, ConvertErrorAction.Delete);
AutoTaggingSettings autoTaggingSettings = new AutoTaggingSettings();
autoTaggingSettings.setEnableAutoTagging(true);
autoTaggingSettings.setHeadingRecognitionStrategy(HeadingRecognitionStrategy.Auto);
options.setAutoTaggingSettings(autoTaggingSettings);
document.convert(options);
document.save(outputFile.toString());
}
}
Create a tagged PDF with an accessible form field
This example tags a signature form field so it becomes part of the logical structure tree.
- Create a new PDF Document and add a page with a form field.
- Add the form field to the document form collection.
- Create a tagged form structure element, associate it with the field, and save the document.
public static void createPdfWithTaggedFormField(Path outputFile) {
try (Document document = new Document()) {
Page page = document.getPages().add();
ITaggedContent taggedContent = document.getTaggedContent();
StructureElement rootElement = taggedContent.getRootElement();
SignatureField signatureField = new SignatureField(page, new Rectangle(50, 50, 100, 100, true));
signatureField.setPartialName("Signature1");
signatureField.setAlternateName("signature 1");
Form formFields = document.getForm();
formFields.add(signatureField);
FormElement form = taggedContent.createFormElement();
form.setAlternativeText("form 1");
form.tag(signatureField);
rootElement.appendChild(form, true);
document.save(outputFile.toString());
}
}
Create a tagged PDF with a TOC page
Use this example when a tagged PDF should include a basic table-of-contents page linked to document headings.
- Create a new tagged PDF Document and add a TOC page.
- Create the TOCElement and a header that should appear in the TOC.
- Link the TOC entry to the heading and save the document.
public static void createPdfWithTocPage(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent content = document.getTaggedContent();
StructureElement rootElement = content.getRootElement();
content.setLanguage("en-US");
Page tocPage = document.getPages().add();
tocPage.setTocInfo(new TocInfo());
TOCElement tocElement = content.createTOCElement();
rootElement.appendChild(tocElement, true);
document.getPages().add();
HeaderElement header = content.createHeaderElement(1);
header.setText("1. Header");
rootElement.appendChild(header, true);
TOCIElement toci = content.createTOCIElement();
tocElement.appendChild(toci, true);
header.addEntryToTocPage(tocPage, toci);
toci.addRef(header);
document.save(outputFile.toString());
}
}
Create an advanced tagged PDF with a TOC page
This example builds a more complex tagged TOC with linked page titles, nested list items, and multiple heading levels.
- Create a new tagged PDF Document and prepare a TOC page with a visible title.
- Create the TOC structure, link the TOC title and entries to headings and list items, and add the related content elements.
- Save the final document with the advanced TOC structure.
public static void createPdfWithTocPageAdvanced(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent content = document.getTaggedContent();
StructureElement rootElement = content.getRootElement();
content.setLanguage("en-US");
Page tocPage = document.getPages().add();
tocPage.setTocInfo(new TocInfo());
tocPage.getTocInfo().setTitle(new TextFragment("Table of Contents"));
TOCElement tocElement = content.createTOCElement();
HeaderElement headerForTocPageTitle = content.createHeaderElement(1);
tocElement.linkTocPageTitleToHeaderElement(tocPage, headerForTocPageTitle);
rootElement.appendChild(headerForTocPageTitle, true);
rootElement.appendChild(tocElement, true);
document.getPages().add();
HeaderElement header = content.createHeaderElement(1);
header.setText("1. Header");
rootElement.appendChild(header, true);
TOCIElement toci = content.createTOCIElement();
tocElement.appendChild(toci, true);
header.addEntryToTocPage(tocPage, toci);
toci.addRef(header);
ListElement listElement = content.createListElement();
for (int i = 1; i < 4; i++) {
ListLIElement li = content.createListLIElement();
listElement.appendChild(li, true);
HeaderElement subHeader = content.createHeaderElement(2);
subHeader.getStructureTextState().setFontSize(Nullable.of(14.0f));
subHeader.setLanguage("en-US");
subHeader.setText("1." + i + " subheader ");
subHeader.addEntryToTocPage(tocPage, li);
li.addRef(subHeader);
ParagraphElement p = content.createParagraphElement();
p.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
p.setLanguage("en-US");
rootElement.appendChild(subHeader, true);
rootElement.appendChild(p, true);
}
toci.appendChild(listElement, true);
HeaderElement header2 = content.createHeaderElement(1);
header2.setText("2. Header");
rootElement.appendChild(header2, true);
TOCIElement toci2 = content.createTOCIElement();
tocElement.appendChild(toci2, true);
header2.addEntryToTocPage(tocPage, toci2);
toci2.addRef(header2);
document.save(outputFile.toString());
}
}