java iTextPdf:动态更改单元格宽度
我正在使用iTextPdf为pdf创建一个表。每页上有9到15列,确切的数字直到运行时才知道。iTextPDF非常适合在页面宽度上创建大小相同的列。但我不知道如何创建不同宽度的列
我不能使用固定的列宽,因为我想跨越整个页面宽度。因此,当写9列时,每列必然比写12或15列时宽。固定的是这些列宽之间的关系。举个例子,我知道A列的宽度永远是B列宽度的75%,B列的宽度永远是C列宽度的50%。我可以为每一列确定这一点
有人知道如何划分页面以正确调整这些列的大小吗?下面是我正在使用的一些代码,它们可以创建大小相同的列。快结束的时候我还需要点别的
cell.setColspan(1);
更改列的宽度,但不更改为固定值。谢谢!
public static void newPDF() throws DocumentException, IOException {
PdfWriter writer;
Document document;
int cols = 9; //can be either 9, 12, or 15
document = new Document();
writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
document.add(createTable(cols));
document.close();
}
public static PdfPTable createTable(int cols) {
PdfPTable table = new PdfPTable(cols);
PdfPCell cell;
for (int i = 0; i < cols; i++) {
ph = selector.process("some text");
cell = new PdfPCell(ph);
cell.setColspan(1); //repeated 9, 12, or 15 times
table.addCell(cell);
}
return table;
}
共 (0) 个答案