gpt4 book ai didi

itext - 表格单元格水平对齐被忽略/损坏

转载 作者:行者123 更新时间:2023-12-02 08:19:25 29 4
gpt4 key购买 nike

我正在使用 iText 7.0.0(Java 风格),似乎表格单元格 Horizo​​ntalAlignment 被忽略,因为 CENTER 和 RIGHT 都不起作用。你能重现这个吗?

see the pdf screenshoot

和重现的代码:

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();
}

最佳答案

请看 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 如下所示:

enter image description here

对齐没有问题,垂直,水平或文本对齐都没有问题。

关于itext - 表格单元格水平对齐被忽略/损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38683813/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com