在iText java pdf中设置表格单元格宽度
我正在使用开源,创建一些pdf 我可以创建一个表,但所有列的宽度都相同,我需要更改特定列的宽度在iText java pdf中设置表格单元格宽度,java,itext,Java,Itext,我正在使用开源,创建一些pdf 我可以创建一个表,但所有列的宽度都相同,我需要更改特定列的宽度 PdfPTable table = new PdfPTable(6); Phrase tablePhrse = new Phrase("Sl n0", normalFontBold); PdfPCell c1 = new PdfPCell(tablePhrse); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.ad
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);
我找不到任何方法来设置列的宽度,请建议一些方法来实现这一点。您可以使用setWidths()方法
如果
table.setWidths(新的int[]{200,50}),这里是正确的一个代码>不起作用
innertable.setWidthPercentage(50);
试试这个
float[] columnWidths = new float[]{10f, 20f, 30f, 10f};
table.setWidths(columnWidths);
确保设置表格总宽度
// 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});
您可以使用column.setPreferredWidth(100)代码>我在PdfTable或PdfCell下找不到.setPreferredWidth(100)方法。抱歉,我误解了。我认为您可以使用table.setTotalWidth()代码>和表.setWidths()代码>谢谢。。Setwidths工作正常。数组中的ITEN数必须与PdfPTable中定义的列数相同constructor@dellasavia谢谢你指出这一点,但这是一个例子。你认为你的代码在接受的答案中的优势是什么?这个代码可以回答这个问题,提供关于此代码为什么和/或如何回答此问题的附加上下文可提高其长期价值。
// 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});
float[] columnWidths = {1, 5, 5};
// columnWidths = {column1, column2, column3...}
PdfPTable table = new PdfPTable(columnWidths)