How to add inline spacing in a PdfPCell?

Tags: tablescellseparatoriText 7

I need to add space between two parts of a line in a PdfPCell.

Chunk chunk = new Chunk("Received  Rs(In Words) :    " + " " + myDataTable.Tables[1].Rows[0]["Recieved"].ToString(), font8);
PdfPTable PdfPTable = new PdfPTable(1);                    
PdfPCell PdfPCell =new PdfPCell(new Phrase(Chunk ));
PdfPCell .Border = PdfCell.NO_BORDER;
PdfPTable .AddCell(PdfPCell );
Posted on StackOverflow on Jul 9, 2015 by S.P

Please take a look at the following screenshot:

Possible options
Possible options

Based on your code snippet, I assume that you want to separate "Received Rs (in Words):" from "Priceless", but it's not clear to me what you want to happen if there is more than one line, so I'm proposing 3 options in the hope that one of them solves your problem. You can find these options in the CellWithGlue example.

Example 1:

table = new Table(2);
table.setBorder(new SolidBorder(1));

cell = new Cell().add(new Paragraph("Received Rs (in Words):"));

cell = new Cell().add(new Paragraph("Priceless"));

In this example, I put "Received Rs (in Words):" and "Priceless" in two different cells, and I align the content of the first cell to the left (by default) and the content of the second cell to the right. This creates space between the two Chunks.

Example 2

// same code as above, except for:

I decreased the width of the table to show you what happens if some content doesn't fit a cell. As we didn't define any widths for the columns, the two columns will have an equal width, but as "Received Rs (in Words):" needs more space than "Priceless", the text doesn't fit the width of the cell and it is wrapped. We could avoid this, by defining a larger with for the first column when compared to the second column.

Example 3:

table = new Table(1);
Paragraph p = new Paragraph();
p.add(new Text("Received Rs (In Words):"));
p.addTabStops(new TabStop(1000, TabAlignment.RIGHT));
p.add(new Tab());
p.add(new Text("Priceless"));
table.addCell(new Cell().add(p));

This example is very close to what you have, but instead of introducing space characters to create space, I introduce a special chunk: new TabStop()). This chunk will separate the two parts by introducing as much space as possible.

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