问题
I have created a PDF but the image is appearing to the middle of the page and I cannot figure out how to align the image and the cell of text to the left of the page.
This is my code
PdfWriter.GetInstance(mydoc, New FileStream(filename, FileMode.Create))
mydoc.Open()
Dim titleTable As New Table(2, 1)
titleTable.Border = 0
titleTable.BorderWidth = 0
titleTable.Cellpadding = 3
titleTable.SetWidths({20, 80})
titleTable.DefaultCellBorder = iTextSharp.text.Rectangle.NO_BORDER
titleTable.TableFitsPage = True
Dim myCell As Cell
'add impero logo
Dim imperoImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(GetBytesForImage(<my image>))
imperoImage.ScalePercent(15)
myCell = New iTextSharp.text.Cell(imperoImage)
myCell.SetHorizontalAlignment(Cell.ALIGN_LEFT)
titleTable.AddCell(myCell)
'title
Dim myChunk As New iTextSharp.text.Chunk(ImperoClientApp.LanguageResources.GetPhrase("some text", Nothing), _
iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 16, iTextSharp.text.Font.BOLD, New iTextSharp.text.Color(0, 0, 0)))
myCell = New iTextSharp.text.Cell(myChunk)
myCell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
titleTable.AddCell(myCell)
mydoc.Add(titleTable)
回答1:
If you turn your table borders back on you'll see that the table cell is as far left as the table allows:
titleTable.Border = 1
titleTable.BorderWidth = 1
''//titleTable.DefaultCellBorder = iTextSharp.text.Rectangle.NO_BORDER
But you also need to tell the table to take up as much horizontal space as possible:
titleTable.Width = 100
However, all of this will still be relative to the document's size including margins. Depending on what you're doing you could just remove the margins from the document completely when you create it:
Dim Doc As New Document(PageSize.LETTER, 0, 0, 0, 0)
NOTE!
The iTextSharp.text.Table
is very old and has been unsupported for almost a decade. Instead you are encouraged to use iTextSharp.text.pdf.PdfPTable
. Almost all documentation out there will always be about the PdfPTable
. Upgrading your code shouldn't be too painful as they have mostly similar methods and properties.
来源:https://stackoverflow.com/questions/17611876/i-cannot-align-my-table-to-the-left-of-the-page