在iText7中,是否有类似iText5的calculateHeights方法的方法? [英] In iText7, is there a way like iText5's calculateHeights method?
问题描述
在iText5中,当我们需要"public float computeHeights(boolean firsttime)"时,可以获取PdfPTable的高度.
但是在iText7中,我们如何获取当前表格的高度值(特别是在将表格添加到其父元素之前)?
我已经测试过"table.getHeight()"方法,但是它返回null. 而且我还发现,在表渲染对象中我可以获取该值,但局限性在于,当表添加到其父元素中时需要触发渲染,所以时间不是我的需要.
因为有时我们需要此值进行计算才能确定"y轴"值.
在iText5中,元素和有关其位置/大小的信息有点混合在一起,这使您可以在PdfPTable
元素上调用calculateWidths
. /p>
在iText7中,此功能是分开的,这为呈现/布局元素提供了不同类型的灵活性.
因此,模型元素(以Table
实例为例)对它们的位置或大小一无所知.调用table.getHeight
会导致null
,因为table
之前没有为其设置HEIGHT
属性.
要计算桌子高度,必须使用渲染功能.
对于模型元素,您可以获取表示该模型元素及其所有子元素的渲染器的子树,并在任何给定区域中获取layout
.要真正知道表格的高度,您需要创建一个区域,该区域将足以放置元素的全部内容.
PdfDocument pdfDoc = ...
Document doc = ...
Table table = new Table(2)
.addCell(new Cell().add(new Paragraph("cell 1, 1")))
.addCell(new Cell().add(new Paragraph("cell 1, 2")));
LayoutResult result = table.createRendererSubTree().setParent(doc.getRenderer()).layout(
new LayoutContext(new LayoutArea(1, new Rectangle(0, 0, 400, 1e4f))));
System.out.println(result.getOccupiedArea().getBBox().getHeight());
上面的代码为我打印了22.982422O
,但是结果可能会有所不同,具体取决于元素的配置和属性.
我想指出代码的两个重要部分:
-
我们将
1e4f
作为LayoutArea
的高度传递,因为这足以放置整个桌子.请注意,如果无法将桌子放到该高度,则结果将永远不会超过该给定高度,因此对于您的用例而言,它是不正确的(知道桌子的总高度).因此,请确保传递足以放置整个桌子的高度. -
.setParent(doc.getRenderer())
部分在这里很重要,用于检索继承属性.请注意,我们没有为table
元素设置很多属性,甚至没有为字体设置任何属性,但是此信息对于了解此元素将占用的区域至关重要.因此,此信息将在layout
期间从父链继承.您可以通过更改文档的字体:document.setFont(newFont);
或字体大小:document.setFontSize(24);
并观察由此产生的高度变化进行测试
In iText5, we can get the PdfPTable's height when we need "public float calculateHeights(boolean firsttime)".
But in iText7, how can we get current table height value (especially before adding the table to its parent element)?
I already tested "table.getHeight()" method, but it returns null. And I also found that in a table render object I can get this value, but the limitation is that the render need to be triggered when the table is adding into its parent element, so the time is not my need.
Cause sometimes we need this value for calculation to decide the "y-axis" value.
In iText5, elements and information about their position/size were a bit mixed together, which allowed you to call calculateWidths
on a PdfPTable
element.
In iText7, this functionality is separated, which allows different kind of flexibility for rendering/layouting elements.
Thus, model elements, which a Table
instance is an example of, do not know anything about their position or size. And calling table.getHeight
results in null
because table
did not have HEIGHT
property previously set to it.
To calculate table height, one would have to make use of the rendering functionality.
For a model element, you can get the subtree of renderers representing this model element and all its children, and layout
it in any given area. To really know the height of a table, you would want to create an area which knowingly will be sufficient to place the whole contents of the element.
PdfDocument pdfDoc = ...
Document doc = ...
Table table = new Table(2)
.addCell(new Cell().add(new Paragraph("cell 1, 1")))
.addCell(new Cell().add(new Paragraph("cell 1, 2")));
LayoutResult result = table.createRendererSubTree().setParent(doc.getRenderer()).layout(
new LayoutContext(new LayoutArea(1, new Rectangle(0, 0, 400, 1e4f))));
System.out.println(result.getOccupiedArea().getBBox().getHeight());
The code above prints 22.982422O
for me, but the results may vary depending on the configuration and properties of elements.
I would like to point out two important parts of the code:
We pass
1e4f
as the height of theLayoutArea
, considering that this will be sufficient to place the whole table. Note that if the table cannot be placed into that height, the result will never exceed this given height and thus it will not be correct for your usecase (know the total height of the table). So make sure to pass the height which will be sufficient for placement of the whole table..setParent(doc.getRenderer())
part is important here and is used for retrieving inheritant properties. Note that we did not set a lot of properties totable
element, even font, but this information is essential to know the area this element would occupy. So this information will be inherited from the parent chain duringlayout
. You can test this by changing the document's font:document.setFont(newFont);
, or font size:document.setFontSize(24);
and watching the resultant height change
这篇关于在iText7中,是否有类似iText5的calculateHeights方法的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!