ASP.NET 2.0 Resources

Powered by Blogger

Generating PDF files with iTextSharp and ASP.NET 2.0

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";
   3:   
   4:  PdfReader reader = new PdfReader(formFile);
   5:  PdfStamper stamper = new PdfStamper(reader, 
   6:       new FileStream(newFile, FileMode.Create));
   7:  AcroFields fields = stamper.AcroFields;
   8:   
   9:  // set form fields
  10:  fields.SetField("TextValue", "John Doe");
  11:  fields.SetField("TextValue2", "2 Milky Way, London");
  12:   
  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;
  19:   
  20:  // flatten form fields and close document
  21:  stamper.FormFlattening = true;
  22:  stamper.Close();

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.

6 Comments:

  • Really cool!

    But what if I have a loooong text that must span over multiple pages? What do you do then?

    By Blogger MFS, at 12:37 PM  

  • I am doing this but writing to the Response stream from an asp.net page. I am running into problems where the form fields in the PDF are not getting populated. The PDF form was generated in adobe designer 7. Any thoughts?

    By Blogger sigreg, at 8:40 PM  

  • Adobe inDesign create fields with special nomenclature. The name of the field goes wrapped by a bunch of weird info, which is not detected by iTextSharp.

    You will need to open it up on adobe professional and work on the form.

    iTextSharp works fine with adobe professional 7.0 forms.

    By Blogger Joao Araujo, at 4:43 AM  

  • This works great for me for text form fields in the pdf, but I am having some difficulty setting checkboxes on/off.

    I've been staring at the iTextSharp class files but I can't seem to figure it out.

    Is there an easy way to set them?

    By Blogger daddyrat, at 10:10 PM  

  • This post has been removed by the author.

    By Blogger peter, at 10:36 AM  

  • hi! is there are a way to export image to pdf using iTexsharp

    By Blogger peter, at 10:39 AM  

Post a Comment

<< Home

Created dolly