我正在使用iTextPdf构建一个pdf表.每个页面上都有9到15列,确切的数字直到运行时才知道.iTextPDF非常适合在页面宽度上创建大小相同的列.但我无法弄清楚如何创建不同宽度的列.
我不能使用固定的列宽,因为我想跨越整个页面宽度.因此,当写入9列时,每列必须比写入12或15列时更宽.固定的是这些列宽度之间的关系.举个例子,我知道A列总是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;
}
Run Code Online (Sandbox Code Playgroud)
我找到了答案.
float[] columnWidths = new float[] {10f, 20f, 30f, 10f};
table.setWidths(columnWidths);
Run Code Online (Sandbox Code Playgroud)
此代码以与括号内的值相对的比例分配列的水平空间(例如,col A是col b的大小的1/2和col C的大小的1/3).
这方面的好例子是:http://www.kodejava.org/examples/833.html
不管怎么说,还是要谢谢你!
| 归档时间: |
|
| 查看次数: |
16735 次 |
| 最近记录: |