How to Create PDF Document Using ITextSharp in Asp.Net C#

PDF Generate

DEMO





C# Coding


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class PDF : System.Web.UI.Page
{

    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;

    public void dbcon()
    {
        string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);
        con.Open();

    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(new Paragraph("Welcome to dotnetdrizzles"));
        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition""attachment;" +
                                       "filename=sample.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();


    }
}




First Create new Webform  - insert values to Database Table - show the data from Gridview


Next download Itextsharp from  below link



http://sourceforge.net/projects/itextsharp/files/latest/download 



Above Link  Download  itextsharp.dll  File to your  Computer


















Select Web form-Select Add Reference - Browse & select iTextsharp.dll file







 Next - Itextsharp Files add to bin









          Next - ItextSharp  Library files Calls to Namespaces







                                      Server Render control










PDF Generate Coding







Click - F5 Button  - Run the program







Select - PDF Generate Button 



OUTPUT






                                                                   

0 comments:

Post a Comment