无法正确计算itext PdfPTable / PdfPCell高度 [英] unable to calculate itext PdfPTable/PdfPCell height properly

查看:1043
本文介绍了无法正确计算itext PdfPTable / PdfPCell高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试生成PdfPTable并在将其添加到文档之前计算其高度时遇到问题。 PdfPTable的方法 calculateHeights 返回的高度远大于页面的高度(而表格大约是页面高度的1/4),所以我写了一个方法计算高度:

I'm facing a problem while trying to generate a PdfPTable and calculate its height before adding it to a document. The method calculateHeights of PdfPTable returned the height a lot greater than the height of a page (while the table is about 1/4 of page's height), so I wrote a method to calculate the height:

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高度,而页面上看起来相同。除了带有块的段落外,单元格中没有任何内容:

where getPdfObject method returns a PdfPTable object.
Using debugger I've discovered that lly and ury coordinate difference (and thus the height) of cell's rectangle is much bigger than it looks after adding a table to a document (for example, one cell is 20 and the other is 38 height while they look like the same on a page). There is nothing in the cell except a paragraph with a chunk in it:

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中间步骤和对象,并不是很有用,我猜... ...
提前谢谢!

A table then has a cell added and setWidthPercentage attribute set to a some float. What am I doing wrong? Why does cell's proportions are different from those I see after generating PDF? Maybe I'm calculating the height wrong? Isn't it the height of a cell on a PDF page should strictly be the difference between lly and ury coordinates
Sorry I haven't shown the exact code, because the PDF is being generated of XML using lots of intermediate steps and objects and it is not very useful "as is" I guess...
Thanks in advance!

推荐答案

添加到可用宽度为400的页面的表格高度与添加到可用宽度为1000的页面的表格高度不同。您无法正确测量高度直到定义宽度。

The height of table added to a page where the available width is 400 is different from the height of a table added to a page where the available width is 1000. There is no way you can measure the height correctly until the width is defined.

可以通过将表格添加到文档来定义宽度。渲染表格后,总高度已知。

Defining the width can be done by adding the table to the document. Once the table is rendered, the total height is known.

如果您想提前知道高度,则需要提前定义宽度。例如,使用:

If you want to know the height in advance, you need to define the width in advance. For instance by using:

table.setTotalWidth(400);
table.setLockedWidth(true);

这在 TableHeight 示例。在 table_height.pdf 中,您会看到iText的返回高度为0 添加表格之前添加表格,并在添加表格后高度为48 。 iText最初返回0,因为无法确定实际高度。

This is explained in the TableHeight example. In table_height.pdf, you see that iText returns a height of 0 before adding a table and a height of 48 after adding the table. iText initially returns 0 because there is no way to determine the actual height.

然后我们采用相同的表格,我们定义总宽度为50(远小于页面上原始80%的可用宽度)。现在,当我们计算具有相同内容的表的高度时,iText返回192而不是48.当您查看页面上的表时,高度差异的原因是显而易见的。

We then take the same table and we define a total width of 50 (which is much smaller than the original 80% of the available width on the page). Now when we calculate the height of the table with the same contents, iText returns 192 instead of 48. When you look at the table on the page, the cause of the difference in height is obvious.

这篇关于无法正确计算itext PdfPTable / PdfPCell高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆