How to add a table in the top-right corner of the page?

I'm creating a table in PDF using iText. I want to place it in top right corner.

25th November 2015

 Here is my code snippet:

PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(160 / 5.23f);
PdfPCell cell = new PdfPCell(new Phrase(" Date" , NORMAL));
PdfPCell cellTwo = new PdfPCell(new Phrase("10/01/2015"));
When I execute this code, the table is aligned at the bottom-right of the page, but i want it on top-right corner.

Posted on StackOverflow on Oct 30, 2015 by user3242119

You have omitted the line that actually adds the table to the document.

Suppose that you have:


In that case, iText will add it at the current position of the cursor. If no content was added yet, the table will be added at the top right. The top right is determined by the top margin and the right margin, but if those aren't 0, you may have the impression that the table isn't added at the top right.

You could also have:

PdfContentByte canvas = writer.getDirectContent();
table.writeSelectedRows(0, -1, document.right() - tablewidth,, canvas);

However, in that case you'd have to define the width of the table differently:


I don't know how wide you want your table. You're using a rather odd formula to define the width percentage.

I wrote the RightCornerTable example in an attempt to reproduce:

Table in upper-right corner

Table in upper-right corner

As you can see, I can't reproduce the problem: the table is shown in the top-right corner.


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