Work with Tables in Tagged PDFs in Java
Tagged table APIs let you create accessible table structures with explicit headers, body rows, footers, and per-cell semantics.
Create a tagged table
Use this example when you need a basic accessible table with header, body, footer, and table summary metadata.
- Create a new tagged PDF Document and add a TableElement.
- Configure the table border and populate the content with the shared helper method.
- Set the table summary attribute and save the document.
public static void createTable(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
taggedContent.setTitle("Example table");
taggedContent.setLanguage("en-US");
TableElement tableElement = taggedContent.createTableElement();
taggedContent.getRootElement().appendChild(tableElement, true);
tableElement.setBorder(new BorderInfo(BorderSide.All, 1.2f, Color.getDarkBlue()));
fillTable(tableElement, 50, 4, true);
StructureAttributes tableAttributes = tableElement.getAttributes().getAttributes(AttributeOwnerStandard.Table);
StructureAttribute summaryAttribute = new StructureAttribute(AttributeKey.Summary);
summaryAttribute.setStringValue("The summary text for table");
tableAttributes.setAttribute(summaryAttribute);
document.save(outputFile.toString());
}
}
Style a tagged table
This example applies table-level formatting such as colors, borders, column sizing, repeating rows, and alignment.
- Create a new tagged PDF Document and add a table element.
- Configure the table-level visual and layout settings.
- Populate the table and save the document.
public static void styleTable(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
taggedContent.setTitle("Example table style");
taggedContent.setLanguage("en-US");
TableElement tableElement = taggedContent.createTableElement();
taggedContent.getRootElement().appendChild(tableElement, true);
tableElement.setBackgroundColor(Color.getBeige());
tableElement.setBorder(new BorderInfo(BorderSide.All, 0.80f, Color.getGray()));
tableElement.setAlignment(HorizontalAlignment.Center);
tableElement.setBroken(TableBroken.Vertical);
tableElement.setColumnAdjustment(ColumnAdjustment.AutoFitToWindow);
tableElement.setColumnWidths("80 80 80 80 80");
tableElement.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.50f, Color.getDarkBlue()));
tableElement.setDefaultCellPadding(new MarginInfo(16.0, 2.0, 8.0, 2.0));
tableElement.getDefaultCellTextState().setForegroundColor(Color.getDarkCyan());
tableElement.getDefaultCellTextState().setFontSize(8.0f);
tableElement.setDefaultColumnWidth("70");
tableElement.setBordersIncluded(true);
tableElement.setLeft(0.0f);
tableElement.setTop(40.0f);
tableElement.setRepeatingColumnsCount(2);
tableElement.setRepeatingRowsCount(3);
TextState rowStyle = new TextState();
rowStyle.setBackgroundColor(Color.getLightCoral());
tableElement.setRepeatingRowsStyle(rowStyle);
fillTable(tableElement, 10, 5, false);
document.save(outputFile.toString());
}
}
Style tagged table rows
Use this example when each row should have its own metadata, borders, height settings, and cell defaults.
- Create a new tagged PDF Document and add table sections for head, body, and foot.
- Create rows and configure their row-level settings such as border, padding, height, and page behavior.
- Populate the rows with cells and save the document.
public static void styleTableRow(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
taggedContent.setTitle("Example table style");
taggedContent.setLanguage("en-US");
TableElement tableElement = taggedContent.createTableElement();
taggedContent.getRootElement().appendChild(tableElement, true);
TableTHeadElement tableTHeadElement = tableElement.createTHead();
TableTBodyElement tableTBodyElement = tableElement.createTBody();
TableTFootElement tableTFootElement = tableElement.createTFoot();
TableTRElement headTrElement = tableTHeadElement.createTR();
headTrElement.setAlternativeText("Head Row");
for (int colIndex = 0; colIndex < 3; colIndex++) {
headTrElement.createTH().setText("Head " + colIndex);
}
for (int rowIndex = 0; rowIndex < 7; rowIndex++) {
TableTRElement trElement = tableTBodyElement.createTR();
trElement.setAlternativeText("Row " + rowIndex);
trElement.setBackgroundColor(Color.getLightGoldenrodYellow());
trElement.setBorder(new BorderInfo(BorderSide.All, 0.75f, Color.getDarkGray()));
trElement.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.50f, Color.getBlue()));
trElement.setMinRowHeight(100.0);
trElement.setFixedRowHeight(120.0);
trElement.setInNewPage(rowIndex % 3 == 1);
trElement.setRowBroken(true);
TextState cellTextState = new TextState();
cellTextState.setForegroundColor(Color.getRed());
trElement.setDefaultCellTextState(cellTextState);
trElement.setDefaultCellPadding(new MarginInfo(16.0, 2.0, 8.0, 2.0));
trElement.setVerticalAlignment(VerticalAlignment.Bottom);
for (int colIndex = 0; colIndex < 3; colIndex++) {
trElement.createTD().setText("Cell [" + rowIndex + ", " + colIndex + "]");
}
}
TableTRElement footTrElement = tableTFootElement.createTR();
footTrElement.setAlternativeText("Foot Row");
for (int colIndex = 0; colIndex < 3; colIndex++) {
footTrElement.createTD().setText("Foot " + colIndex);
}
document.save(outputFile.toString());
}
}
Style tagged table cells
This example uses the shared helper method to create a table with cell-level formatting and merged cells.
- Create a new tagged PDF Document.
- Add a table element and populate it through the helper method with cell styling enabled.
- Save the document.
public static void styleTableCell(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
taggedContent.setTitle("Example table cell style");
taggedContent.setLanguage("en-US");
TableElement tableElement = taggedContent.createTableElement();
taggedContent.getRootElement().appendChild(tableElement, true);
fillTable(tableElement, 4, 4, true);
document.save(outputFile.toString());
}
}
Adjust tagged table position
Use this example when a tagged table should be positioned explicitly on the page.
- Create a new tagged PDF Document and add a table element.
- Configure PositionSettings for the table.
- Apply the position settings, populate the table, and save the document.
public static void adjustTablePosition(Path outputFile) {
try (Document document = new Document()) {
ITaggedContent taggedContent = document.getTaggedContent();
taggedContent.setTitle("Example table position");
taggedContent.setLanguage("en-US");
TableElement tableElement = taggedContent.createTableElement();
taggedContent.getRootElement().appendChild(tableElement, true);
PositionSettings positionSettings = new PositionSettings();
positionSettings.setHorizontalAlignment(HorizontalAlignment.None);
positionSettings.setMargin(new MarginInfo(20, 0, 0, 0));
positionSettings.setVerticalAlignment(VerticalAlignment.None);
positionSettings.setFirstParagraphInColumn(false);
positionSettings.setKeptWithNext(false);
positionSettings.setInNewPage(false);
positionSettings.setInLineParagraph(false);
tableElement.adjustPosition(positionSettings);
fillTable(tableElement, 4, 4, true);
document.save(outputFile.toString());
}
}
Fill a tagged table with structured content
This helper method creates the header, body, and footer rows for a table, and optionally applies cell styling and spans.
- Create the table head, body, and foot sections.
- Populate header, body, and footer rows with accessible cell elements.
- Optionally configure styled cells, merged cells, and text state values.
private static void fillTable(TableElement tableElement, int rowCount, int colCount, boolean styleCells) {
TableTHeadElement tableTHeadElement = tableElement.createTHead();
TableTBodyElement tableTBodyElement = tableElement.createTBody();
TableTFootElement tableTFootElement = tableElement.createTFoot();
TableTRElement headTrElement = tableTHeadElement.createTR();
headTrElement.setAlternativeText("Head Row");
headTrElement.setBackgroundColor(Color.getLightGray());
for (int columnIndex = 0; columnIndex < colCount; columnIndex++) {
TableTHElement thElement = headTrElement.createTH();
thElement.setText("Head " + columnIndex);
thElement.setBackgroundColor(Color.getGreenYellow());
thElement.setBorder(new BorderInfo(BorderSide.All, 4.0f, Color.getGray()));
thElement.setNoBorder(true);
thElement.setMargin(new MarginInfo(16.0, 2.0, 8.0, 2.0));
thElement.setAlignment(HorizontalAlignment.Right);
}
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
TableTRElement trElement = tableTBodyElement.createTR();
trElement.setAlternativeText("Row " + rowIndex);
for (int columnIndex = 0; columnIndex < colCount; columnIndex++) {
int colSpan = 1;
int rowSpan = 1;
if (styleCells && columnIndex == 1 && rowIndex == 1) {
colSpan = 2;
rowSpan = 2;
} else if (styleCells && ((rowIndex == 1 && columnIndex == 2)
|| (rowIndex == 2 && (columnIndex == 1 || columnIndex == 2)))) {
continue;
}
TableTDElement tdElement = trElement.createTD();
tdElement.setText("Cell [" + rowIndex + ", " + columnIndex + "]");
tdElement.setBackgroundColor(Color.getYellow());
tdElement.setBorder(new BorderInfo(BorderSide.All, 4.0f, Color.getGray()));
tdElement.setNoBorder(false);
tdElement.setMargin(new MarginInfo(8.0, 2.0, 8.0, 2.0));
tdElement.setAlignment(HorizontalAlignment.Center);
TextState cellTextState = new TextState();
cellTextState.setForegroundColor(Color.getDarkBlue());
cellTextState.setFontSize(7.5f);
cellTextState.setFontStyle(FontStyles.Bold);
cellTextState.setFont(FontRepository.findFont("Arial"));
tdElement.setDefaultCellTextState(cellTextState);
tdElement.setWordWrapped(true);
tdElement.setVerticalAlignment(VerticalAlignment.Center);
tdElement.setColSpan(colSpan);
tdElement.setRowSpan(rowSpan);
}
}
TableTRElement footTrElement = tableTFootElement.createTR();
footTrElement.setAlternativeText("Foot Row");
footTrElement.setBackgroundColor(Color.getLightSeaGreen());
for (int columnIndex = 0; columnIndex < colCount; columnIndex++) {
TableTDElement tdElement = footTrElement.createTD();
tdElement.setText("Foot " + columnIndex);
tdElement.setAlignment(HorizontalAlignment.Center);
tdElement.getStructureTextState().setFontSize(com.aspose.pdf.Nullable.of(7.0f));
tdElement.getStructureTextState().setFontStyle(com.aspose.pdf.Nullable.of(FontStyles.Bold));
}
}