Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning text in iText in a single row

Tags:

java

itext

What is the best way to create a row of text that will have two elements aligned to an imaginary line? Like this (four rows given to illustrate the point better):

   1. some random text
  34. some more random text
 764. here's even more random text
4594. it just never ends

Imaginary line would go trough the dots, or the space after them. The numbers have right alignment, and the text has left alignment.

I do not want to use a list because elements may not be in order, and it has certain limitation with setting the line spacing.

like image 220
Karlovsky120 Avatar asked Oct 29 '12 22:10

Karlovsky120


1 Answers

You can use a PdfPTable with 2 columns, the first right aligned and the last left aligned. Then set the designer padding on the cells content. For example:

PdfPTable tbl = new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Phrase("1."));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(Rectangle.BOX);
tbl.addCell(cell);
cell = new PdfPCell(new Phrase("some random text"));
cell.disableBorderSide(Rectangle.BOX);
tbl.addCell(cell);
cell = new PdfPCell(new Phrase("34."));
cell.disableBorderSide(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
tbl.addCell(cell);
cell = new PdfPCell(new Phrase("some more random text"));
cell.disableBorderSide(Rectangle.BOX);
tbl.addCell(cell);

You can see that cell border are disabled (disableBorderSide method). You can also adjust the minimum height of cells using setMinimumHeight method.

like image 125
Pier Luigi Avatar answered Nov 14 '22 03:11

Pier Luigi