deepak deepak - 1 year ago 87 Question

how to enter text to new line in iTextshar PDF

I have written following code to write text in PDF and i want to break the line after some text .

Dim document As Document

document = New Document(PageSize.A4, 5.0F, 20.0F, 20.0F, 20.0F)


Dim writer As PdfWriter
writer = PdfWriter.GetInstance(document, New FileStream(filename, FileMode.Create))


Dim spacing As Integer

spacing = 0

Dim curY, lineHeight As Double

curY = document.Top
lineHeight = 0

Const maxPerLine As Integer = 3

For i As Integer = 0 To 5

Dim table As PdfPTable
table = New PdfPTable(4)

table.DefaultCell.Border = Rectangle.NO_BORDER
table.TotalWidth = 200.0F
table.LockedWidth = True

Dim cell As PdfPCell
cell = New PdfPCell(New Phrase("hello \n" + i + "\n" + "wass up ?" ))
cell.Colspan = 4
cell.HorizontalAlignment = 0
cell.Border = Rectangle.NO_BORDER
cell.Padding = 30.0F

table.WriteSelectedRows(0, -1, document.Left + spacing, curY, writer.DirectContent)

spacing = spacing + 200

lineHeight = Math.Max(lineHeight, table.TotalHeight)

If 0 = (i + 1) Mod maxPerLine Then

curY = curY - lineHeight
spacing = 0
lineHeight = 0

End If


Catch ex As Exception



End Try

i have tried with Paragraph but still i am not able to enter texts into new line.

I have read the doc of iTextSharp they have written if you want to break line then use "\n" but it is not working.

How can i break line after some text ?

Answer Source

The easiest way, is to use PdfPCell in composite mode (you're using text mode). Composite mode gets into play when you use AddElement:

Dim cell As PdfPCell
cell = New PdfPCell()
cell.AddElement(New Paragraph("line 1"))
cell.AddElement(New Paragraph("line 2"))

Note that you can't set the alignment at the level of the PdfPCell in this case. When using composite mode, you have to set the alignment at the level of the elements (in this case at the level of the Paragraph).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download