Discussion:
[iText-questions] Text Alignment Within Cell - iTextSharp
john
2011-03-28 00:03:02 UTC
Permalink
I am using iTextSharp 5.06 with VB2010. I am using Sumatra 1.3 and
Foxit Reader 4.3 as my PDF viewer.

I am creating one textcolumn, containing one table consisting of one
cell. I want to put some short text in that cell, and have it
vertically align bottom and horizontally align right.

If I try to set the cell horizontal alignment to anything but
ALIGN_LEFT (or leave the alignment left by default) the text I put in
the cell disappears. The cell vertical alignment works fine, just the
horizontal alignment makes the text disappear.

I read all of the iText documentation available, and everything but
the horizontal alignment works. Am I still not doing something quite
right when I set up my table or column?


--- code follows below ---


Private Sub CreateTextLabel()
Dim StartX As Single = 20 ' printing offsets from 0,0
Dim StartY As Single = 20

Doc.SetMargins(0, 0, 0, 0) ' make a PDF with NO MARGINS

Dim FilePath As String = Application.StartupPath() + "\temp\"
+ "temp.pdf"
Dim FileStream As FileStream = New FileStream(FilePath,
FileMode.Create)
Dim writer As PdfWriter = PdfWriter.GetInstance(Doc,
FileStream)
Doc.Open()

Dim cb As PdfContentByte = writer.DirectContent()
Dim ct As ColumnText
Dim table As PdfPTable
Dim cell As PdfPCell
Dim font As Font
Dim para As Paragraph

FontFactory.RegisterDirectories() ' call this ONCE in this
'sub to initialize the system fonts

' create new instances each time we draw
ct = New ColumnText(cb)
cell = New PdfPCell
font = FontFactory.GetFont("Times New Roman",
BaseFont.WINANSI, BaseFont.EMBEDDED,
12, font.NORMAL, New BaseColor(Color.FromName("Black")))

para = New Paragraph("This is some text", font)
table = New PdfPTable(1) ' 1 table with 1 cell

' Create new TextColumn
ct.SetSimpleColumn(StartX, StartY, StartX + 300, StartY + 200)

'set up properties of the stuff to draw
table.TotalWidth = 300
table.LockedWidth = True ' lock width to absolute value


' text is blank when anything but ALIGN_LEFT
cell.HorizontalAlignment = Element.ALIGN_RIGHT

cell.VerticalAlignment =
Element.ALIGN_BOTTOM
cell.NoWrap = True
' make table, cell and text column same width, width
'must always be FIXED
cell.FixedHeight = leftspinewidthTextBox.Text
cell.Rotation = 0

' connect elements together and print it
cell.AddElement(para)
table.AddCell(cell)
ct.AddElement(table)
ct.Go()
End Sub
1T3XT BVBA
2011-03-28 08:57:08 UTC
Permalink
Post by john
I read all of the iText documentation available,
So you are aware of the difference between "text mode" and "composite mode".
Post by john
' text is blank when anything but ALIGN_LEFT
cell.HorizontalAlignment = Element.ALIGN_RIGHT
' connect elements together and print it
cell.AddElement(para)
The documentation clearly states that you shouldn't mix text mode and
composite mode!
Switching from text mode to composite mode can result in data loss!
Loading...