Table 单元格 HorizontalAlignment 是 ignored/broken
Table Cell HorizontalAlignment is ignored/broken
我正在使用 iText 7.0.0(Java 风格),似乎 table 单元格 HorizontalAlignment 被忽略了,因为 CENTER 和 RIGHT 都不起作用。你能重现这个吗?
以及要重现的代码:
private static void brokenTableCellHorizontalAlignmentPdf(OutputStream output) throws IOException {
PdfWriter writer = new PdfWriter(output);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
Table table = new Table(new float[] {15f, 16f, 4f}).setWidthPercent(100);
for (int y = 1; y <= 3; ++y) {
for (int x = 1; x <= 3; ++x) {
table.addCell(
new Cell()
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setHorizontalAlignment(HorizontalAlignment.CENTER)
.add(new Paragraph(String.format("(%d, %d)%s", y, x, x == 1 ? "\n\ntest" : ""))
.setFont(font)
.setFontSize(8)));
}
}
document.add(table);
document.close();
}
哦没关系!查看另一个答案(指向 http://gitlab.itextsupport.com/itext7/samples/blob/develop/publications/highlevel/src/main/java/com/itextpdf/highlevel/chapter05/C05E03_CellAlignment.java)后,我们现在应该使用 setTextAlignment
。如:
new Paragraph(String.format("(%d, %d)%s", y, x, x == 1 ? "\n\ntest" : ""))
.setFont(font)
.setFontSize(8)
.setTextAlignment(TextAlignment.CENTER)
请看一下CellAlignment例子:
public void createPdf(String dest) throws IOException {
//Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// Initialize document
Document document = new Document(pdf);
Table table = new Table(new float[]{2, 1, 1});
table.setWidthPercent(80);
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.setTextAlignment(TextAlignment.CENTER);
table.addCell(new Cell(1, 3).add("Cell with colspan 3"));
table.addCell(new Cell(2, 1).add("Cell with rowspan 2")
.setTextAlignment(TextAlignment.RIGHT));
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
Cell cell = new Cell()
.add(new Paragraph("Left").setTextAlignment(TextAlignment.LEFT))
.add(new Paragraph("Center"))
.add(new Paragraph("Right").setTextAlignment(TextAlignment.RIGHT));
table.addCell(cell);
cell = new Cell().add("Middle")
.setVerticalAlignment(VerticalAlignment.MIDDLE);
table.addCell(cell);
cell = new Cell().add("Bottom")
.setVerticalAlignment(VerticalAlignment.BOTTOM);
table.addCell(cell);
document.add(table);
document.close();
}
当您 运行 此示例时生成的 PDF 如下所示:
对齐没有问题,无论是垂直对齐、水平对齐还是文本对齐。
我正在使用 iText 7.0.0(Java 风格),似乎 table 单元格 HorizontalAlignment 被忽略了,因为 CENTER 和 RIGHT 都不起作用。你能重现这个吗?
以及要重现的代码:
private static void brokenTableCellHorizontalAlignmentPdf(OutputStream output) throws IOException {
PdfWriter writer = new PdfWriter(output);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
Table table = new Table(new float[] {15f, 16f, 4f}).setWidthPercent(100);
for (int y = 1; y <= 3; ++y) {
for (int x = 1; x <= 3; ++x) {
table.addCell(
new Cell()
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setHorizontalAlignment(HorizontalAlignment.CENTER)
.add(new Paragraph(String.format("(%d, %d)%s", y, x, x == 1 ? "\n\ntest" : ""))
.setFont(font)
.setFontSize(8)));
}
}
document.add(table);
document.close();
}
哦没关系!查看另一个答案(指向 http://gitlab.itextsupport.com/itext7/samples/blob/develop/publications/highlevel/src/main/java/com/itextpdf/highlevel/chapter05/C05E03_CellAlignment.java)后,我们现在应该使用 setTextAlignment
。如:
new Paragraph(String.format("(%d, %d)%s", y, x, x == 1 ? "\n\ntest" : ""))
.setFont(font)
.setFontSize(8)
.setTextAlignment(TextAlignment.CENTER)
请看一下CellAlignment例子:
public void createPdf(String dest) throws IOException {
//Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// Initialize document
Document document = new Document(pdf);
Table table = new Table(new float[]{2, 1, 1});
table.setWidthPercent(80);
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.setTextAlignment(TextAlignment.CENTER);
table.addCell(new Cell(1, 3).add("Cell with colspan 3"));
table.addCell(new Cell(2, 1).add("Cell with rowspan 2")
.setTextAlignment(TextAlignment.RIGHT));
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
Cell cell = new Cell()
.add(new Paragraph("Left").setTextAlignment(TextAlignment.LEFT))
.add(new Paragraph("Center"))
.add(new Paragraph("Right").setTextAlignment(TextAlignment.RIGHT));
table.addCell(cell);
cell = new Cell().add("Middle")
.setVerticalAlignment(VerticalAlignment.MIDDLE);
table.addCell(cell);
cell = new Cell().add("Bottom")
.setVerticalAlignment(VerticalAlignment.BOTTOM);
table.addCell(cell);
document.add(table);
document.close();
}
当您 运行 此示例时生成的 PDF 如下所示:
对齐没有问题,无论是垂直对齐、水平对齐还是文本对齐。