Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Create PDF file in ASP.NET

Viewed:  1704 
Posted On:  25/01/2013 10:51:34 
In this article I am going to show how we can create PDF file in 

In this article I am going to show how we can create pdf file in ASP.NET. For this I used a dll of itextsharp.  Like shown in below figure...


Figure 1.

This is my

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Create PDF  Page</title>
    <form id="form1" runat="server">
        <table cellpadding="4" cellspacing="4" width="60%" align="center" border="2" style="border-color: Blue;">
                    Your Text To Create PDF
                    <table cellpadding="0" cellspacing="1" width="90%" align="center" border="2" style="border-color: Blue;">
                                <asp:TextBox ID="txtForPDF" runat="server" TextMode="MultiLine" Width="700px" Height="200px"></asp:TextBox>
                        </tr>                        <tr>
                                <asp:Label ID="lblMessage" runat="server" ForeColor="Red" Visible="false"></asp:Label>
                <td align="center">
                    <asp:Button ID="btnCreatePDF" runat="server" Text="Create PDF" OnClick="btnCreatePDF_Click"
                        Width="180px" Height="50px" />

This is my aspx.cs code

using System;
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.Xml.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void btnCreatePDF_Click(object sender, EventArgs e)
        var myDoc = new Document();
        string path = Server.MapPath("PDF");
        PdfWriter.GetInstance(myDoc, new FileStream(path + "/MyInfo.pdf", FileMode.Create));
        myDoc.Add(new Paragraph(txtForPDF.Text));
        txtForPDF.Text = "";
        lblMessage.Text = "PDF created successfully...";
        lblMessage.Visible = true;

When run the application then UI


Figure 2.

When click on Create PDF button

Figure 3.



         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us