Trabajar con tablas en Tagged PDFs en Java
Las APIs de tablas etiquetadas le permiten crear estructuras de tabla accesibles con encabezados explícitos, filas del cuerpo, pies de página y semántica por celda.
Crear una tabla etiquetada
Utilice este ejemplo cuando necesite una tabla accesible básica con encabezado, cuerpo, pie de página y metadatos de resumen de la tabla.
- Cree un nuevo Tagged PDF
Documenty agregue unTableElement. - Configure el borde de la tabla y rellene el contenido con el método auxiliar compartido.
- Establezca el atributo de resumen de la tabla y guarde el documento.
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());
}
}
Estilizar una tabla etiquetada
Este ejemplo aplica formato a nivel de tabla, como colores, bordes, dimensionado de columnas, filas repetidas y alineación.
- Cree un nuevo Tagged PDF
Documenty agregue un elemento de tabla. - Configure la configuración visual y de diseño a nivel de tabla.
- Rellene la tabla y guarde el documento.
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());
}
}
Estilizar filas de tabla etiquetadas
Utilice este ejemplo cuando cada fila deba tener sus propios metadatos, bordes, configuraciones de altura y valores predeterminados de celda.
- Cree un nuevo Tagged PDF
Documenty agregue secciones de tabla para encabezado, cuerpo y pie. - Cree filas y configure sus ajustes a nivel de fila, como el borde, el relleno, la altura y el comportamiento de la página.
- Llene las filas con celdas y guarde el documento.
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());
}
}
Estilo de celdas de tabla etiquetadas
Este ejemplo usa el método auxiliar compartido para crear una tabla con formato a nivel de celda y celdas combinadas.
- Cree un nuevo Tagged PDF
Document. - Agregue un elemento de tabla y rellénelo mediante el método auxiliar con el estilo de celda habilitado.
- Guarde el documento.
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());
}
}
Ajustar posición de la tabla etiquetada
Utilice este ejemplo cuando una tabla etiquetada deba posicionarse explícitamente en la página.
- Cree un nuevo Tagged PDF
Documenty agregue un elemento de tabla. - Configure
PositionSettingspara la tabla. - Aplique la configuración de posición, rellenar la tabla y guarde el documento.
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());
}
}
Rellenar una tabla etiquetada con contenido estructurado
Este método auxiliar crea las filas de encabezado, cuerpo y pie de tabla, y opcionalmente aplica estilo a las celdas y fusiones.
- Cree la cabecera, el cuerpo y el pie de la tabla.
- Rellene filas de encabezado, cuerpo y pie de página con elementos de celda accesibles.
- Opcionalmente configure celdas con estilo, celdas combinadas y valores de estado de texto.
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));
}
}