iText pdf library
Website search

How to add an inner table surrounded by text?

I am trying to add a table surrounded by text to an outer table, but the inner table disappears and I can't seem to fix the problem.

Here is what I am expecting:


* Hello World       *
* +++++++++++++++++ * 
* + Goodbye World + *  these 3 lines never show up in the PDF
* +++++++++++++++++ * 
* Hello World       *
Here is my code example:


final PdfPTable table2 = new PdfPTable(1);
table2.addCell("Goodbye World");
final PdfPTable table1 = new PdfPTable(1);
Phrase phrase = new Phrase();
phrase.add(new Chunk("Hello World"));
phrase.add(table2); // 
phrase.add(new Chunk("Hello World"));
Posted on StackOverflow on Feb 10, 2015 by rveach

Please take a look at the NestedTableProblem example:

// table 2
Table table2 = new Table(1);
table2.addCell(new Cell().setBorder(new SolidBorder(Color.RED, 1)).add("Goodbye World"));
// table 1
Table table1 = new Table(1);
Cell cell = new Cell();
cell.setBorder(new SolidBorder(Color.BLACK, 1));
cell.add("Hello World");
cell.add("Hello World");

In this code snippet, we compose the Cell object of different elements and add it to the main table. Here is the result:

Screen shot

Screen shot

Note that we use Table and Cell classes in iText 7 instead of PdfPTable and PdfPCell.

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

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