0

my question is how to make text in new line I tried "\n", but its didn't work. My code:

 Dim cell1 As New PdfPCell(New Phrase("line 1 \n line2 \n line3"))
    cell1.Border = 0
    cell1.HorizontalAlignment = 0
    table.AddCell(cell1)
Stuneris
  • 77
  • 2
  • 12

2 Answers2

2

Use CHR(10) instead of using the C# '\' escape character.

Paulo Soares
  • 1,896
  • 8
  • 21
  • 19
0

You are currently creating a PdfPCell using text mode. This is meant for simple text only. See Right aligning text in PdfPCell

As soon as you have more text and maybe other types of content such as images, it may be better to use composite mode and to create a cell with different lines by using different Paragraph objects instead of a Phrase (a Phrase isn't supposed to have newlines).

Your code would then look like this:

Dim cell1 As New PdfPCell()
cell1.Border = PdfPCell.NO_BORDER
Dim p1 As New Paragraph("line 1");
p1.Alignment = Element.ALIGN_LEFT;
cell.AddElement(p1);
Dim p2 As New Paragraph("line 2");
p1.Alignment = Element.ALIGN_CENTER
cell.AddElement(p2)
Dim p3 As New Paragraph("line 3");
p1.Alignment = Element.ALIGN_RIGHT
cell.AddElement(p3)
table.AddCell(cell1)

Do yourself (and especially the people who will have to maintain your code) a favor and never use lines such as:

cell1.Border = 0

Replace this by:

cell1.Border = PdfPCell.NO_BORDER

The same goes for:

cell1.HorizontalAlignment = 0

Replace this by:

cell1.HorizontalAlignment = Element.ALIGN_LEFT

You will notice that the people who have to read your code will start respecting you more, because writing code that is easy for them to read and understand is proof that you respect them.

Note: a PdfPCell switches from text mode to composite mode as soon as you use the AddElement() method. In composite mode, the following line will be ignored:

cell1.HorizontalAlignment = Element.ALIGN_LEFT

In composite mode, the cell will use the alignment of the different components in favor of the alignment defined at the level of the cell.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165