I use iText 7
with html2pdf
2.1.2
version and try to convert HTML file to PDF.
I use HtmlConverter#convertToElements
to process an html file. Then I would like to calculate the width of one of the elements (a table in particular).
I have tried to implement custom TagWorker
class, but i get table only after it's filled.
Also tried convertToDocument
and some other ways but with no success.
Is there a way to get a table width at run time?
Thanks in advance.
The bad news: in general, it's not possible to get the real width of the table until it has been placed. It highly depends on the layout area.
The good news: one can simulate table's layout and check the occupied place:
In the code above, I've created a renderer for the table (
createRendererSubTree()
), set its parent (setParent(doc.getRenderer())
- this line is important for correct properties processing) and layouted it (layout(new LayoutContext(new LayoutArea(0, area)))
;UPD: I've read your initial question and have another solution for you. Suppose that you don't have any area to place, but want to see how much space the table could occupy.
Use the next snippet then:
In the code above I've calculated the min and the max width of the table.