3
我正在使用iTextPdf爲PDF生成表格。每個頁面上會有9到15列,直到運行時間才知道確切的數字。 iTextPDF非常適合在整個頁面寬度上創建大小相同的列。但我無法弄清楚如何創建不同寬度的列。iTextPdf:動態更改單元格寬度
我不能使用固定列寬,因爲我想跨越整個頁面寬度。因此,寫入9列時,每列的寬度必須比寫入12列或15列的寬度要寬。固定的是這些列寬度之間的關係。舉個例子,我知道列A將始終是列B寬度的75%,這將始終是列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;
}