0

I need to add Space between two lines in iTextSharp 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 );
Sudipta
  • 4,773
  • 2
  • 27
  • 42
S.P
  • 1
  • 1
  • 1
  • Space between two lines in the context of PDF is called *leading*, There have been a lot of questions and answers about setting the leading in cells (also search for *text mode* versus *composite mode*). However, when I look at your code snippet, I only see one line. Is it possible that you don't want to add space between two lines, but between two chunks that are *on the same line*? Please clarify, otherwise you risk getting an answer about leading, which may not be what you need. – Bruno Lowagie Jul 09 '15 at 11:17
  • Thanks for your Replay, But some time it will have more than one line – S.P Jul 09 '15 at 11:41
  • As explained in the comment to my answer, I've made three guesses trying to figure out wha is meant with "inline space": (1) space separating two parts of text on the same line, one to the left, the other to the right, (2) space between the lines (the *leading* that separates consecutive line), (3) non-breaking space (` `) inside the line. According to the OP none of these guesses were correct. This question either needs clarification, or it needs to be closed as "unclear what is asked". – Bruno Lowagie Jul 10 '15 at 12:05
  • You never gave any feedback informing me if my answer solved your problem. – Bruno Lowagie Nov 25 '15 at 13:58

1 Answers1

3

Please take a look at the following screen shot:

enter image description here

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 have written 3 examples:

Example 1:

table = new PdfPTable(2);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidthPercentage(60);
table.setSpacingAfter(20);
cell = new PdfPCell(new Phrase("Received Rs (in Words):"));
cell.setBorder(PdfPCell.LEFT | PdfPCell.TOP | PdfPCell.BOTTOM);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Priceless"));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setBorder(PdfPCell.RIGHT | PdfPCell.TOP | PdfPCell.BOTTOM);
table.addCell(cell);
document.add(table);

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 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:
table.setWidthPercentage(50);

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 PdfPTable(1);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidthPercentage(50);
Phrase p = new Phrase();
p.add(new Chunk("Received Rs (In Words):"));
p.add(new Chunk(new VerticalPositionMark()));
p.add(new Chunk("Priceless"));
table.addCell(p);
document.add(table);

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

If this doesn't answer your question, you were probably looking for the concept known as leading. Leading is the space between the baseline of two lines of text. If that is the case, please read the following Q&As:

If your question isn't about leading, then maybe you're just looking for the concept known as non-breaking space:

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you for your Answer, BY this code we can add space between two tables or two columns. But i need to add inline space in a cell – S.P Jul 10 '15 at 08:30
  • @S.P Please define "inline space". My guesses were: (1) space separating two parts of text, one to the left, the other to the right, (2) space between the lines (*leading*), (3) non-breaking space inside the line. Apparently, I guess wrong 3 times, so if you want me to help you, you will have to clarify. I don't think I'm the only person who thinks this question isn't clear. – Bruno Lowagie Jul 10 '15 at 12:02
  • Inline space means, The space between two lines in a cell or in a paragraph – S.P Jul 16 '15 at 07:06