Thursday, May 8, 2014

How to Write to PDF using itextsharp in Asp.Net

Leave a Comment
In this example let us see how to write pdf file using itextSharp library in asp.net using C#. In one of other post I explained about how to create pdf using itextsharp in .net. To write or fill pdf using itextSharp first you need to create form fields on PDF. To create form fields on PDF file use adobe acrobat or any other pdf edit tool.

I used online pdf editor to create text field on PDF file.

Steps to Fill or Write PDF using itextsharp.dll in asp.net C#:

Step 1: Visit url www.pdfescape.com/open in your browser as shown in snapshot:


Step 2: Select the option Upload PDF to PDFescape if you already have pdf file as shown in image below:


Step 3: Now click on Form Field as shown in the screenshot below:


Step 4: Select its type Text if you want to add textbox on the pdf as shown in the image below:


Step 5: Now click  on pdf where you want to add the text filed. Right click on newly  added text filed and then select the option Object Properties as shown in the screen shot below:


Step 6: Give name to your form field in object properties window. Select the option Read Only . Note down the name you have given to form field in notepad. Repeat the same process for other field on the pdf as well.The final pdf will look similar to this:


Step 7: As you done with editing the pdf file on the left click on save& download button as shown in the screeshot below:
Step 8: Create a new project in visual studio and add reference to itextsharp.dll. Download itexsharp if not present already.


Step 9: Create a New Folder PDF in your project and paste pdf file you have downloaded there.Name it to sample.pdf create another copy of this pdf file in the same folder and name it completed_PDF.pdf as shown in the screen shot below:


Step 10: Add a new web form and paste the following HTML code:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="txtWritPdf" Text="Click to Write PDF" runat="server"
 onclick="txtWritPdf_Click" />
 <br />
<asp:Label runat="server" ID="lblfinal" Text="PDf is written successfully" Visible="false"></asp:Label>
</div>
</form>
</body>
</html>

Code Behind:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text; 
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.IO;


Add this code:
protected void Page_Load(object sender, EventArgs e)
    {
        lblfinal.Visible = false;
      
    }
    protected void txtWritPdf_Click(object sender, EventArgs e)
    {
        WritetoPDF();
    }
    private void WritetoPDF()
    {
      
        string template_of_pdf = Server.MapPath("~/PDF/Sample.pdf");
        string NewPdfFile = Server.MapPath("~/PDF/completed_PDF.pdf");

        PdfReader ReadPDF = new PdfReader(template_of_pdf);
        PdfStamper pdfStamper = new PdfStamper(ReadPDF, new FileStream(
                    NewPdfFile, FileMode.Create)); 
        AcroFields PdfNewFiled = pdfStamper.AcroFields;
        ReadPDF.Tampered = false; 
        PdfNewFiled.Fields.IsReadOnly.Equals(true);     
        PdfNewFiled.SetField("txtname", "Manish Kumar");
        PdfNewFiled.SetField("txtssnNumber", "22233");
        PdfNewFiled.SetField("txtmailing", "test");
        PdfNewFiled.SetField("name", "Manish Kumar");
        PdfNewFiled.SetField("txtzip", "123456");
        PdfNewFiled.SetField("txttelephone", "22844747");
        PdfNewFiled.SetField("txtalternatephone", "8865466");
        PdfNewFiled.SetField("txtage", "26");
        PdfNewFiled.SetField("txtemail", "test@a.com");
       
        pdfStamper.Close();
        lblfinal.Visible = true;

    }

In the code above PdfNewFiled.SetField("txtname""Manish Kumar") txtname is the name of the field you have given while created pdf.


Run the project and click on button. If success message is shown in label. Then open the PDF folder in the project and open the file completed_PDF.pdf . The final output is shown below:


   


0 comments:

Post a Comment