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

Create PDF file in ASP.NET

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

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...

SolutionExplorer.png

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Create PDF  Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="4" cellspacing="4" width="60%" align="center" border="2" style="border-color: Blue;">
            <tr>
                <td>
                    Your Text To Create PDF
                </td>
            </tr>
            <tr>
                <td>
                    <table cellpadding="0" cellspacing="1" width="90%" align="center" border="2" style="border-color: Blue;">
                        <tr>
                            <td>
                                <asp:TextBox ID="txtForPDF" runat="server" TextMode="MultiLine" Width="700px" Height="200px"></asp:TextBox>
                            </td>
                        </tr>                        <tr>
                            <td>
                                <asp:Label ID="lblMessage" runat="server" ForeColor="Red" Visible="false"></asp:Label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <asp:Button ID="btnCreatePDF" runat="server" Text="Create PDF" OnClick="btnCreatePDF_Click"
                        Width="180px" Height="50px" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 

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.Open();
        myDoc.Add(new Paragraph(txtForPDF.Text));
        myDoc.Close();
        txtForPDF.Text = "";
        lblMessage.Text = "PDF created successfully...";
        lblMessage.Visible = true;
 
    }
} 

When run the application then UI

UI1.png

Figure 2.

When click on Create PDF button

UI2.png
Figure 3.

UI3.png

Figure4.

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