在 Java 中处理 Tagged PDF 表格
Contents
[
Hide
]
Tagged table APIs 让您能够创建具有显式标题、正文行、页脚以及每个单元格语义的可访问表结构。
创建带标记的表格
当您需要一个带有标题、主体、页脚和表格摘要元数据的基本可访问表格时,请使用此示例。
- 创建一个新的 Tagged PDF Document 并添加一个 TableElement。
- 配置表格边框,并使用共享的帮助方法填充内容。
- 设置表格的 summary 属性并保存文档。
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());
}
}
为标记表格设置样式
此示例应用表级格式设置,例如颜色、边框、列大小、重复行和对齐方式。
- 创建一个新的 Tagged PDF 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());
}
}
为标记的表格行设置样式
在每行需要拥有各自的元数据、边框、高度设置和单元格默认值时,请使用此示例。
- 创建一个新的 Tagged PDF 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());
}
}
为标记的表格单元格设置样式
此示例使用共享的帮助方法创建一个具有单元格级别格式设置和合并单元格的表格。
- 创建一个新的 Tagged PDF 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());
}
}
调整已标记表格位置
在需要将标记表格明确放置在页面上的情况下,请使用此示例。
- 创建一个新的 Tagged PDF Document 并添加一个表格元素。
- 配置 PositionSettings 用于表格。
- 应用位置设置,填充表格,并保存文档。
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());
}
}
用结构化内容填充标记表格
此辅助方法为表创建表头、表体和表脚行,并可选地应用单元格样式和跨越。
- 创建表头、主体和页脚部分。
- 使用可访问的单元格元素填充标题、正文和页脚行。
- 可选地配置样式化单元格、合并单元格和文本状态值。
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));
}
}