How to make table fits the pdf page using iText?

How to make table fits the pdf page using iText?

or

How to fit PdfPTable in a page?

You might be wondering when adding table to the pdf it doesnot fit to the page horizontally. i.e Table do not use all the available width in the document page. Yes when you add a PDFPTable to the pdf document page, it leaves some margin on left and right side depending upon which alignment is being set.

This is because when we create the table, default width is used. By default this width is 80 percent of the available width. So when we consider the available width it is total width minus margins of left and right hand side. Suppose the width of the page is 595 pt minus the margins, which are 36 pt on each side. So the available width is 523. And table use 80 percent of the total width avaialble which is 418.4 pt.

So if we want to use the total width available on the document for a table we can set the widthPercentage using the setWidthPercentage() method i.e. table.setWidthPercentage(100);
So for example you have created one PDFPtable you need to explicitly set the width percentage using table.setWidthPercentage(100);

i.e.

PdfPTable pTable = new PdfPTable(3);
pTable.setWidthPercentage(100);


Another method in which you can specify the width to be set in the table is setTotalWidth using which we can set the totalWidth of the table.
But totalWidth depends upon one flag which is lockedWidth which is false by default. So by default width of the table depends upon the width available for the table.
If we set lockedWidth as true and specify the width in setTotalWidth then it takes the width as specified.

So when considring table width two properties are really important one is
widthPercentage and second is totalWidth.

So when you want pdf table to use maximum document width then we need to explicitly set width percentage to 100.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment