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

Tags: tablesposition tableiText 5

I'm creating a table in PDF using iText. I want to place it in top right corner. 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.