问题
how to format paragraph string to show content left, Right or middle of pdf document using iTextsharp in visual basic and absolute position on the document.
Thanks
as per suggestion by Bruno Lowagie I am using
Dim table As New PdfPTable(3)
table.setWidthPercentage(100)
table.addCell(getCell("Text to the left", PdfPCell.ALIGN_LEFT))
table.addCell(getCell("Text in the middle", PdfPCell.ALIGN_CENTER))
table.addCell(getCell("Text to the right", PdfPCell.ALIGN_RIGHT))
document.add(table)
Public Function getCell(ByVal text As String, ByVal alignment As Integer) As PdfPCell
Dim cell As New PdfPCell(New Phrase(text))
cell.setPadding(0)
cell.setHorizontalAlignment(alignment)
cell.setBorder(PdfPCell.NO_BORDER)
Return cell
End Function
I am getting error cell.setPadding, cell.setHorizontalAlignment,cell.setBorder all are notmember of iTextsharp.Text.pdf.PdfPCell also table.setWidthPercentage(100) shows error argument not specified parameter 'page size'
回答1:
I am not a visual basic programmer (the last time I used visual basic was in 1996 and I said: never again!), but just by using Google, I adapted your example like this:
Dim table As New PdfPTable(3)
table.WidthPercentage = 100
table.AddCell(GetCell("Text to the left", PdfPCell.ALIGN_LEFT))
table.AddCell(GetCell("Text in the middle", PdfPCell.ALIGN_CENTER))
table.AddCell(GetCell("Text to the right", PdfPCell.ALIGN_RIGHT))
document.Add(table)
Public Function GetCell(ByVal text As String, ByVal alignment As Integer) As PdfPCell
Dim cell As New PdfPCell(New Phrase(text))
cell.Padding = 0
cell.HorizontalAlignment = alignment
cell.Border = PdfPCell.NO_BORDER
Return cell
End Function
This is commonly known:
- Methods in Java start with lower case; methods in .NET start with upper case, so when people ask you to use Java code as pseudo code and to convert Java to .NET, you need to change methods such as
add()
andaddCell()
intoAdd()
andAddCell()
. - Member-variables in Java are changed and consulted using getters and setters; variables in .NET are changed and consulted using methods that look like properties. This means the you need to change lines such as
cell.setBorder(border);
andborder = cell.getBorder();
intocell.Border = border
andborder = cell.Border
.
iText and iTextSharp are kept in sync, which means that, using the two rules explained above, a developer won't have any problem to convert iText code into iTextSharp code.
When in doubt about a method, one can always do as I did, Google for these methods and properties! You'll find examples such as:
- Align itextsharp table
- iTextSharp Table Cell Spacing Possible?
- ...
If you want a whole bunch of examples in one place, download The Best iText Questions on StackOverflow
来源:https://stackoverflow.com/questions/29575793/how-to-format-paragraph-string-to-show-content-left-right-or-middle-of-pdf-docu