在iText java pdf中设置表格单元格宽度

问题描述 投票:9回答:5

我正在使用opensource http://itextpdf.com/来创建一些pdf。

我可以创建一个表,但所有列的宽度都相同,我需要更改特定列的宽度。

PdfPTable table = new PdfPTable(6);
Phrase tablePhrse = new Phrase("Sl n0", normalFontBold);
    PdfPCell c1 = new PdfPCell(tablePhrse);
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

我找不到任何方法来设置列的宽度,请提示一些方法来实现这一点..

java itext
5个回答
22
投票

您可以使用setWidths()方法。

table.setWidths(new int[]{200,50});

public void setWidths(int[] relativeWidths)


2
投票

试试这个。

float[] columnWidths = new float[]{10f, 20f, 30f, 10f};
table.setWidths(columnWidths);

1
投票

如果table.setWidths(new int[]{200,50});不起作用,这是更正的。

innertable.setWidthPercentage(50);

0
投票
float[] columnWidths = {1, 5, 5};

// columnWidths = {column1, column2, column3...}

PdfPTable table = new PdfPTable(columnWidths)

0
投票

确保设置表格总宽度

// Set Column Number
PdfPTable table = new PdfPTable(2);

// Set Table Total Width
table.setTotalWidth(500);

// Set Each Column Width - Make Sure Array is the same number specified in constructor
table.setWidths(new int[]{50, 450});
© www.soinside.com 2019 - 2024. All rights reserved.