MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

ITextSharp Display Text in Separate Lines

I have some text like

1   a
    b
    c
    d

With iTextSharp how do I display this(in separate lines, one after the other)?

MY CODE:

Dim pdftableSLD As PdfPTable = New PdfPTable(3)
pdftableSLD.DefaultCell.Padding = 3
pdftableSLD.WidthPercentage = 96
pdftableSLD.DefaultCell.BorderWidth = 1
pdftableSLD.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT
pdftableSLD.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE
pdftableSLD.DefaultCell.FixedHeight = 40.0F
pdftableSLD.HorizontalAlignment = 1
Dim widthsSLD As Single() = {0.5F, 1.25F, 2.5F}
pdftableSLD.SetWidths(widthsSLD)

Dim stuName As PdfPCell = New PdfPCell(FormatPhrase(""))
stuName.Colspan = 4
stuName.Border = Rectangle.BOTTOM_BORDER
stuName.NoWrap = True
stuName.HorizontalAlignment = Element.ALIGN_CENTER
pdftableMain.AddCell(stuName)

In this table I have to display the text specified above.

Upvotes: 0

Views: 282

Answers (1)

Chris Haas
Chris Haas

Reputation: 55457

Your sample looks tabular and thus probably warrants a table. Two PdfPTable paths come to mind as well as one using Paragraph

Option 1 - Create a normal 2x4 table

''//Create a two column table divided 20%/80%
Dim tbl1 As PdfPTable = New PdfPTable({20, 80})

''//Add cells
tbl1.AddCell("1")
tbl1.AddCell("a")

tbl1.AddCell("")
tbl1.AddCell("b")

tbl1.AddCell("")
tbl1.AddCell("c")

doc.Add(tbl1)

Option 2 - Create a 2x4 table with the first cell spanning down 4 rows

''//Create a two column table divided 20%/80%
Dim tbl2 As PdfPTable = New PdfPTable({20, 80})

''//Add a cell that spans four rows
tbl2.AddCell(New PdfPCell(New Phrase("1")) With {.Rowspan = 4})

''//Add cels normally
tbl2.AddCell("a")
tbl2.AddCell("b")
tbl2.AddCell("c")

doc.Add(tbl2)

Option 3 - Use a paragraph with tab stops

''//Create a normal paragraph
Dim P As New Paragraph()

''//Add first "column"
P.Add("1")

''//Add a tab
P.Add(Chunk.TABBING)

''//Add second "column"
P.Add("a")

''//Soft return
P.Add(vbNewLine)

''//Repeat, starting subsequent items with a tab
P.Add(Chunk.TABBING)
P.Add("b")
P.Add(vbNewLine)

P.Add(Chunk.TABBING)
P.Add("c")
P.Add(vbNewLine)

doc.Add(P)

Upvotes: 1

Related Questions