I have been trying to generate some PDF files from ASP.NET 2.0 application using iTextSharp PDF generator library. I needed to fill predefined PDF templates with data and send them directly to my ASP NET application users. Those templates were created with Adobe Acrobat with value placeholders created as form fields. It's quite easy to fill such PDF templates using iTextSharp and it's much easier as creating whole PDF file from scratch (which is also possible with iTextSharp).
iTextSharp is a port of the iText, a free Java-Pdf library. iText is a library that allows developers to extend the capabilities of their web server applications with dynamic PDF document generation. The latest iTextSharp version (3.1.0) is based on iText 1.4. The tutorials and documentation on iTextSharp homepage are quite out of date but the principles remain the same. iTextSharp 3.1.0 compiles and runs on ASP.NET 2.0 without any major problems.
You will first need the PDF file template for this method. You can easily create the template and form fields with Adobe Acrobat. The next step is quite simple. Read PDF file from disk, create instance of PDFStamper class, fill out the PDF form fields and maybe set some global PDF document properties. Finally flatten the generated PDF file and save it to disk (or stream to client if necessary). Look at the following simplified example for further details.
1: string formFile = @".\FormDocument_src.pdf";
2: string newFile = @".\FormDocument_out.pdf";
4: PdfReader reader = new PdfReader(formFile);
5: PdfStamper stamper = new PdfStamper(reader,
6: new FileStream(newFile, FileMode.Create));
7: AcroFields fields = stamper.AcroFields;
9: // set form fields
10: fields.SetField("TextValue", "John Doe");
11: fields.SetField("TextValue2", "2 Milky Way, London");
13: // set document info
14: Hashtable info = new Hashtable();
15: info["Title"] = "Modified Form";
16: info["Author"] = "Dolly";
17: info["Creator"] = "My Web App";
18: stamper.MoreInfo = info;
20: // flatten form fields and close document
21: stamper.FormFlattening = true;
The PDF file is opened on line 4 using PDFReader class. On the next line PDFStamper class is instantiated and the output PDF file created. On line 7 the AcroFields are read from stamper which is what's interesting for us. There are some PDF form fields filled at lines 10 and 11. The lines 14 to 18 show how to set global PDF document properties. And finally on the line 21 the PDF file is flattened and output stream closed.Read more > Posted on: 4/24/2006 07:52:00 PM