iText 5 is the previous major version of iText’s leading PDF SDK. iText 5 is EOL, and is no longer developed, although we still provide support and security fixes. Switch your project to iText 8, our latest version which supports the latest PDF standards and technologies.
Check related iText 8 content!
Here is my code snippet:
PdfPTable table = new PdfPTable(1); table.setHorizontalAlignment(Element.ALIGN_RIGHT); table.setWidthPercentage(160 / 5.23f); PdfPCell cell = new PdfPCell(new Phrase(" Date" , NORMAL)); cell.setBackgroundColor(BaseColor.BLACK); cell.setBorderWidth(2f); table.addCell(cell); PdfPCell cellTwo = new PdfPCell(new Phrase("10/01/2015")); cellTwo.setBorderWidth(2f); table.addCell(cellTwo);
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, document.top(), 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
As you can see, I can't reproduce the problem: the table is shown in the top-right corner.