Step 1: Add the itextsharp Dll from this link sourceforge.net/projects/itextsharp/
Step 2: Add the following codes:-
Default.aspx
<%@ 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> <formid="form1"runat="server"> <div> <tablecellpadding="4"cellspacing="4"width="60%"align="center"border="2"style="border-color: Blue;"> <tr> <td> Your Text To Create PDF </td> </tr> <tr> <td> <tablecellpadding="0"cellspacing="1"width="90%"align="center"border="2"style="border-color: Blue;"> <tr> <td> <asp:TextBoxID="txtForPDF"runat="server"TextMode="MultiLine"Width="700px"Height="200px"></asp:TextBox> </td> </tr> <tr> <td> <asp:LabelID="lblMessage"runat="server"ForeColor="Red"Visible="false"></asp:Label> </td> </tr> </table> </td> </tr> <tr> <tdalign="center"> <asp:ButtonID="btnCreatePDF"runat="server"Text="Create PDF"OnClick="btnCreatePDF_Click" Width="180px"Height="50px"/> </td> </tr> </table> </div> </form> </body> </html>
Default.aspx.cs
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 { protectedvoid Page_Load(object sender, EventArgs e) { }
protected void btnCreatePDF_Click(object sender, EventArgs e) { var myDoc = newDocument(); string path = Server.MapPath("PDF"); PdfWriter.GetInstance(myDoc, newFileStream(path + "/MyInfo.pdf", FileMode.Create)); myDoc.Open(); myDoc.Add(newParagraph(txtForPDF.Text)); myDoc.Close(); txtForPDF.Text = ""; lblMessage.Text = "PDF created successfully..."; lblMessage.Visible = true; } }