-1

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'

Sunil Bhagwat
  • 137
  • 2
  • 13
  • Welcome to StackOverflow. Please browse around and you'll discover that questions similar to yours usually get the comment *What have you tried?* I am the original developer of iText, so please believe me when I say that there are many different ways to achieve this: using `PdfPTable`, using `ColumnText`, using the low-level API,... However, you shouldn't expect that I write a whole chapter explaining the pros and cons of the different approaches. Instead you should narrow down your question so that a single answer suffices. – Bruno Lowagie Apr 11 '15 at 10:08
  • Incidentally, I see that almost the exact same question was asked 2 hours before you posted your question. I have answered it here: http://stackoverflow.com/questions/29575142/how-to-align-two-paragraphs-or-text-in-left-and-right-in-a-same-line-in-pdf – Bruno Lowagie Apr 11 '15 at 11:12
  • how to use solution 2 in visual basic – Sunil Bhagwat Apr 11 '15 at 14:18
  • You shouldn't have any problem doing that yourself. If you do have a problem, show us what you've tried. If you don't want to show what you've tried, then what are you doing on StackOverflow??? – Bruno Lowagie Apr 11 '15 at 15:00
  • 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) – Sunil Bhagwat Apr 11 '15 at 15:06
  • (1) Do not post source code in a comment field (2) Do not copy my code literally. E.g. is there really a ssetWidthPercentage method in iTextSharp??? I think it's table.WidthPercentage you need. Also: there are no method that start with lower case in iTextSharp!!! (3.) Why do you omit the getCell method??? – Bruno Lowagie Apr 11 '15 at 15:07
  • yes if not what to do – Sunil Bhagwat Apr 11 '15 at 15:09
  • Update your question. If that's impossible, delete this question and post a new one. In its current form, your question is not suited for StackOverflow. It's not because you're a newbie that you should act as one ;-) – Bruno Lowagie Apr 11 '15 at 15:11
  • I have corrected my question – Sunil Bhagwat Apr 11 '15 at 15:26
  • Yes, but you haven't read any of the documentation! **You are copying the JAVA methods!** Why don't you convert them to the corresponding .NET parameters? – Bruno Lowagie Apr 11 '15 at 19:00
  • pl give me documentation about itext to read – Sunil Bhagwat Apr 12 '15 at 04:30
  • The best free book to start with is probably [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html). It contains hundreds of code snippets in Java, C#, etc... Once you've read that book, take a look at our [learn](http://itextpdf.com/learn) page. – Bruno Lowagie Apr 12 '15 at 06:44

1 Answers1

0

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() and addCell() into Add() and AddCell().
  • 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); and border = cell.getBorder(); into cell.Border = border and border = 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:

If you want a whole bunch of examples in one place, download The Best iText Questions on StackOverflow

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165