Sunday, February 19, 2012

Create PDF document using ASP.Net


This program will create PDF document using ASP.Net and C# .I have used iTextSharp free library to generate PDF cocument. Follow the following steps to create a pdf document.



Step 1)Download iTextSharp from following link

  http://webscripts.softpedia.com/scriptDownload/iTextSharp--Download-31157.html

Step 2) Add iTextsharp.dll file as reference of your ASP.Net page

Step 3)Add the following code in your ASP.Net page
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

public partial class pdf_creation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Document myDocument = new Document(PageSize.A4.Rotate());

        try
        {

            PdfWriter.GetInstance(myDocument, new FileStream("D:\\Salman.pdf",  FileMode.Create));
            myDocument.Open();
            myDocument.Add(new Paragraph("First Pdf File made by Salman using iText"));

        }
        catch (DocumentException de)
        {
            Response.Write(de.Message);
        }
        catch (IOException ioe)
        {
            Response.Write(ioe.Message);
        }
        myDocument.Close();
      
    }
}

No comments:

Post a Comment