iText pdf library
Website search

How to maintain a paragraph's indentation inside a table cell?

Some of my paragraph objects contain text that can be greater than the width of the cell in length. These paragraphs are also indented, but the problem is if a paragraph needs to take a new line due to text length, the indenting is not maintained on the new line, causing most of the text to be indented, then the remainder of the text containing no indenting on the new line.

Is there a way to determine if a paragraph will take a new line due to exceeding PdfPCell width, and to indent the remainder of the text if this is true?

Posted on StackOverflow on Jan 21, 2015 by jbailie1991

Please take a look at the SimpleTable4 example. In this example, I add an indented paragraph to a cell the wrong way (your way) and I add a paragraph to a cell the correct way (as explained in the documentation):

PdfPTable table = new PdfPTable(1);
Paragraph wrong = new Paragraph("This is wrong, because an object that was originally a paragraph is reduced to a phrase due to the fact that it's put into a cell that uses text mode.");
PdfPCell wrongCell = new PdfPCell(wrong);
Paragraph right = new Paragraph("This is right, because we create a paragraph with an indentation to the left and as we are adding the paragraph in composite mode, all the properties of the paragraph are preserved.");
PdfPCell rightCell = new PdfPCell();

This is the result:

Cell in text mode, cell in composite mode

Cell in text mode, cell in composite mode

In the first row, we no longer have a Paragraph, we have a Phrase that uses the alignment and the leading of the PdfPCell.

In the second row, the Paragraph is preserved. If an alignment or a leading was defined at the level of the PdfPCell, it is ignored in favor of the alignment and the leading of the Paragraph. All the other properties that are defined at the level of the Paragraph (and that do not exist for Phrase objects) are preserved.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

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