Pages

Tuesday, May 5, 2009

Populating PDF from ASP.Net using iTextSharp

iTextSharp is a very popular third party library which is used by programs that write to PDF files. Suppose there is a requirement where we want to write data into PDF files.Then we use iTextSharp.

The steps in doing this:
1.Download iTextSharp DLL.
2.In Visual Studio,create a project and add iTextSharp DLL as a reference.
3.Then add directive "Imports iTextSharp.text.pdf" to the top of the form.
4. A reader in iTextSharp is represented by the appropriately-named PDFReader object.Then we have to instantiate the PDFReader object.
5.Then we have to get the PDF file into stamper object.
6.Setting PDF form fields.

dim reader as new PdfReader("Specify Pdf path")
Using fs as new FileStream("PDF PATH",FileMode.Create)
Dim stamper as new PDFStamper(reader,fs)
Dim fields as AcroFields=stamper.AcroFields
fields.SetField("PDF Field Name","Data you want to write")

Suppose you have a field in PDF form with ID "txtName"
fields.SetField("txtName","Vijay")




Thats it :)

15 comments:

Mark Jacobs said...

Vijay,
Your post caught my attention; I am new to >NET programing and have done a little Access progroming in the past.
I have started a .NET project Visual Studio 2008 in VB where my form can display, edit and insert data into a MySQL database hosted at godaddy. . Now I need to use the data that is displayed in the form for the selected record to create "certificates" and other forms in PDF format.
Your quick little post seems to be what I am looking for; but I do have some questions please:
1 Is the code in this post compatible with a project in VB?
2 Does this produce a flat PDF or is there other code to render the PDF flat?
3 If i put a button on my form to call the PDF populated with the selected record how would I do that?

Thanks you for your time.

Mark

Vijaya Kadiyala said...

Hi Mark,
Thank you for visiting my blog.
1 Is the code in this post compatible with a project in VB?
==VJ== The Code should work.
2 Does this produce a flat PDF or is there other code to render the PDF flat?
==VJ== could you please explain in detail
3 If i put a button on my form to call the PDF populated with the selected record how would I do that?
==VJ== Have the above code as a Sub Routine and call this OnClick event of the button.

Please let me know if you have any questions.

Mark Jacobs said...

Vijay,

Thank you for responding so quickly. What I mean by a "flattened" PDF is one that cannot be edited only viewed so the end user can only save or print the PDF with the populated fields as shown.

I'm sitting down now to give it go. I will report back to you when I am successful.

Again thank you so much.

Mark

Mark Jacobs said...

OK I set everything up and when I put the code in the on_click event of my button it says that I need a using end statement and that "pdfreader" and "FilesStream" are not defined.



Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
Dim reader As New PdfReader("../../Forms/sltest.pdf")
Using fs As New FileStream("../../Forms/sltest.pdf", FileMode.Create)
Dim stamper As New PDFStamper(reader, fs)
Dim fields As AcroFields = stamper.AcroFields
fields.SetField("pdflast", "@NODLastLabel")
fields.SetField("pdffirst", "@NODFirstLabel")
fields.SetField("pdfdod", "@NODDODLabel")
fields.SetField("pdfhome", "@HomeLabel")
End Using
End Sub
End Class

Vijaya Kadiyala said...

Hi Mark,
Have you download the iTextSharp DLL and added to your project!! and imported as shown in the 1.2.3 steps?

Could you please paste the error with error code!

Mark Jacobs said...

I referenced the dll; the errors were caused by not putting the imports at the top of the aspx.vb file. Now with the code below I have no errors but the pdf that it generates is is only 1k in size and cannot be opened. It seems that it is not completing the creation and merge of the new pdf.


Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.pdf.PdfStamper
Imports System
Imports System.Text



Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

Dim formfile = ("C:\sltest.pdf")
Dim newfile = ("C:\sltest5.pdf")
Dim PdfReader As New PdfReader(formfile)
Using fs As New FileStream((newfile), FileMode.Create)


Dim stamper As New PdfStamper(PdfReader, fs)
Dim fields As AcroFields = stamper.AcroFields
fields.SetField("pdflast", "test")
fields.SetField("pdffirst", "test2")
fields.SetField("pdfdod", "test3")
fields.SetField("pdfhome", "test4")
End Using

Mark Jacobs said...

Vijay,
I got it working - what I needed to do was close the pdfstamper after the merge:

stamper.close()

otherwise it stays open and when you go to open it in a reader it gets corrupted.

This is great stuff and will help me greatly with my project.

Thank You

Vijaya Kadiyala said...

Hi Mark,
Great going :).

Mark Jacobs said...

Vijay,

Do you know a way of inserting the text from an asp label into the setfield command? I can only insert a literal sting like below:

fields.SetField("pdfhome", "test4")

Instead of "test4" to be writen to the PDF field I would like the text from a label on my asp form to be written. I can't figure out the syntax. test4label.text does not work.
Have any ideas?

Mark

Mark Jacobs said...

Vijay,

I found the problem pulling text from my label: The label was a control of a FormView control therefore I needed to declare the label as follows.

dim lastname As Label = formView1.FindControl("NODLastLabel")

now I can pulle the text value from the label like this:

fields.SetField("pdflast", lastname.text)

I hope you don't mind that I have been posting to your blog but I want my experience to help any other noobies that may find this helpful and save them the hours of work that it took me to get to where I am with my project.

Thank you again,


Mark

Vijaya Kadiyala said...

Hi Mark,
just use Lable_Name.Caption to get the label caption.
let me know if it doesn't work...

Petro said...

Hi,
I have an existing progran written in Clarion. How can I Call the itextsharp dll in my program. is it an activex control? Then I can handle it as OLE object?? I first try to write a com wrapper for it. BUT the tpl file shows no methods??
So I guess I can only use it in a .net program?

aditya saraswat said...

hi sir ji,
i know ur name is vijay but i 'll call u sirji.
i am the student of .NET from jaipur. last two days i was searching for iTextSharp DLL.
then i got ur post thanks
it really helpful for me.

--
aad!
--

Vijaya Kadiyala said...

Hi Adi,
Thanks for your comments.

Lee said...

GREAT POST! I never thought I'd get this working... one minor problem. I have to populate a rich text field in the pdf and I used HttpUtility.HtmlAttributeEncode(varName) which worked perfect for the local server of Visual Web Developer 2010, but when I move it to IIS 6/server 2003 the rich text has lost it's html formatting. Any suggestions anyone??

thanks in advanced