SIGN UP MEMBER LOGIN:    
ARTICLE

How to use iTextSharp .Net Library to insert text to PDF Templates

Posted by Lasantha PW Articles | Learn .NET January 23, 2011
In this article you will learn how to use iTextSharp .Net Library to insert text to PDF Templates.
Reader Level:

You can use Adobe Acrobat professional to create a PDF form template. Put a TextField and give a name for that as text1.Then set the formatting.

In the c# code follow these steps

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Xml;
using System.Xml.Linq;

/* reference to the itextsharp */
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;

namespace ItextSharpTut
{
    public class UtilityMethod
    {

        public void generatePDF()
        {
            try
            {

                string pdfTemplate = System.Web.HttpContext.Current.Server.MapPath("~/doc/new.pdf");

                PdfReader pdfReader = null;
                // Create the form filler
                FileStream pdfOutputFile = new FileStream(pdfTemplate, FileMode.Create);

                string path = System.Web.HttpContext.Current.Server.MapPath("~/doc/template.pdf");
                pdfReader = new PdfReader(path);

                PdfStamper pdfStamper = null;

                pdfStamper = new PdfStamper(pdfReader, pdfOutputFile);

                // Get the form fields
                AcroFields testForm = pdfStamper.AcroFields;
                testForm.SetField("text1", "Lasantha");
                PdfContentByte overContent = pdfStamper.GetOverContent(1);

                pdfStamper.FormFlattening = true;

                pdfStamper.Close();

                pdfReader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}


Enjoy it.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor