【发布时间】:2014-08-18 08:33:55
【问题描述】:
我在尝试生成 PdfPTable 并在将其添加到文档之前计算其高度时遇到问题。 PdfPTable的calculateHeights方法返回的高度比页面高度大很多(而表格大约是页面高度的1/4),所以我写了一个计算高度的方法:
protected Float getVerticalSize() throws DocumentException, ParseException, IOException {
float overallHeight=0.0f;
for(PdfPRow curRow : this.getPdfObject().getRows()) {
float maxHeight = 0.0f;
for(PdfPCell curCell : curRow.getCells()) {
if(curCell.getHeight()>maxHeight) maxHeight=curCell.getHeight();
}
overallHeight+=maxHeight;
}
return overallHeight;
}
getPdfObject 方法返回一个 PdfPTable 对象。
使用调试器,我发现单元格矩形的 lly 和 ury 坐标差异(以及高度)比将表格添加到文档后看起来要大得多(例如,一个单元格是 20,另一个是 38 高度,而他们在页面上看起来相同)。单元格中除了一个带有块的段落之外什么都没有:
Font f = getFont();
if (f != null) {
int[] color = getTextColor();
if(color != null) f.setColor(color[0],color[1],color[2]);
ch = new Chunk(celltext, f);
par = new Paragraph(ch);
}
cell = new PdfPCell(par);
cell.setHorizontalAlignment(getHorizontalTextAlignment());
cell.setVerticalAlignment(getVerticalTextAlignment());
然后表格添加了一个单元格并将setWidthPercentage 属性设置为某个浮点数。
我究竟做错了什么?为什么单元格的比例与我生成 PDF 后看到的比例不同?也许我计算的高度错误?难道不是PDF页面上单元格的高度严格来说应该是lly和ury坐标之间的差异
抱歉,我没有显示确切的代码,因为 PDF 是使用大量中间步骤和对象由 XML 生成的,我猜它“按原样”不是很有用...
提前致谢!
【问题讨论】:
标签: java height itext pdfptable