Joseph Michael Pesch
VP Programming

Open Source PDF Generator iTextSharp

by 23. July 2010 17:19

iTextSharp-SampleC#WebSite.zipx (2.49 mb)

itextsharp-5.0.6-dll.zip (1.19 mb)

Sample code to populate form fields in template and save as new document (via C# web page):

using System;

using System.IO;

using iTextSharp.text.pdf;

 

publicpartialclassFillFormFields : System.Web.UI.Page

{

  protectedvoid Page_Load(object sender, EventArgs e)

  {

    GeneratePDF();

  }

 

  protectedvoid GeneratePDF()

  {

    // Setup the output file

    string pdfOutputFilePath = @"Output\FillFormFields-"

      + DateTime.Now.ToString()

      .Replace("/", "_")

      .Replace(":", "_")

      .Replace(" ", "_")

      + ".pdf";

    FileStream pdfOutputFile

      = newFileStream(Server.MapPath(pdfOutputFilePath)

        , FileMode.Create, FileAccess.ReadWrite);

    // Load the template file

    PdfReader pdfReader = newPdfReader(Server.MapPath(@"Templates\FillFormFields.pdf"));

    PdfStamper pdfStamper = null;

    pdfStamper = newPdfStamper(pdfReader, pdfOutputFile);

    // Get the form fields object

    AcroFields pdfForm = pdfStamper.AcroFields;

    // Fill the form fields with data

    pdfForm.SetField("Lead.LastAction.Date", DateTime.Now.ToString("MMMM dd, yyyy"));

    // Close the documents

    pdfStamper.FormFlattening = true;

    pdfStamper.Close();

    pdfReader.Close();

  }

}

 

 

 

Tags:

ASP.Net | C#

Comments are closed