unable to calculate itext PdfPTable/PdfPCell height properly

走远了吗. 提交于 2019-12-12 14:49:07

问题


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;
}

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());

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!


回答1:


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);

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.

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.




回答2:


Inorder to get dynamic table height we should set and lock width of table. Here, 595 is A4 size paper width.

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


来源:https://stackoverflow.com/questions/24448319/unable-to-calculate-itext-pdfptable-pdfpcell-height-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!