How to resize a PdfPTable to fit the page?

I am generating a document that contains a table. I need to make sure that this table never exceeds one page in size, regardless of the amount of content in the cells. Is there a way to do this?

31st May 2016
admin-marketing

I am generating a document that contains a table. I need to make sure that this table never exceeds one page in size, regardless of the amount of content in the cells. Is there a way to do this?

Posted on StackOverflow on Feb 19, 2013 by Jannis Alexakis

If you want a table to fit a page, you should create the table before even thinking about page size and ask the table for its height, determining firstly the necessary width of the table. This can be done like this:

IRenderer tableRenderer = table.createRendererSubTree().setParent(doc.getRenderer());
LayoutResult tableLayoutResult = tableRenderer.layout(new LayoutContext(new LayoutArea(0, new Rectangle(width, 1000))));
float tableHeightTotal = tableLayoutResult.getOccupiedArea().getBBox().getHeight();

Now you can create a Document with size PageSize(width + margin * 2, tableHeightTotal + margin * 2) and the table should fit the document exactly.

Click this link if you want to see how to answer this question in iText 5.


Share this article

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now