Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

How to create a PDF file from a web form dynamically using ASP.NET?

Hello Experts,

I am creating a web form, which is used to insert values into the SQL server database.My requirement is that I need to create a button to print the form (which has all the values entered by the user). Please tell me how can i create a pdf file from the web form using ASP.NET

I need to do this urgently, please help.

  • 2
  • 2
1 Solution
Try This:


<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="CrystalDecisions.CrystalReports.Engine" %>
<%@ import Namespace="CrystalDecisions.Shared" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Text" %>
<script runat="server">

      public void Page_Load(Object sender, EventArgs E) {

            //*** INSTRUCTIONS:
            //define action for this program
            //1. set action to "CreateSchemaFile"
            //2. run program which creates your .xsd schema file
            //3. create crystal report based on that file
            //4. then set action permanently to "CreatePdf"

            //the command
            string action = "CreatePdf";

            string tableName = "fin";
            string rptFile = "fin.rpt";
            string xsdFile = "fin.xml";
            string pdfFile = "fin.pdf";

            //either create the schema (first time only) or the pdf file
            if(action.ToUpper() == "CREATESCHEMAFILE") {
                  DataTable dt = new DataTable();
                  dt = DummyTable();
                  DataSet ds = new DataSet();
                  Response.Write("Your schema file has been created:<br/><br/> <b>" + Server.MapPath(xsdFile) + "</b><br/><br/>");
                  Response.Write("Use it to create your Crystal Reports report file named:<br/><br/> <b>" + Server.MapPath(pdfFile) + "</b>");
            } else {
                  //create the report document
                  ReportDocument doc = new ReportDocument();
                  string fileName = Server.MapPath(rptFile);

                  DataTable dt = DummyTable();
                  dt.TableName = tableName;
                  DataSet ds = new DataSet();
                  dt.TableName      = "Table";


                  ExportOptions exportOpts = doc.ExportOptions;
                  exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
                  exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
                  exportOpts.DestinationOptions = new DiskFileDestinationOptions();

                  // Set the disk file options.
                  DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
                  ( ( DiskFileDestinationOptions )doc.ExportOptions.DestinationOptions ).DiskFileName = Server.MapPath("fin.pdf");

                  Response.Write("<a href=\"" + pdfFile + "\">" + pdfFile + "</a>");


      private void CreateSchemaFile(string fileName, DataSet ds) {
            string absoluteFileName = HttpContext.Current.Server.MapPath(fileName);
            FileStream myFileStream = new FileStream (absoluteFileName, FileMode.Create);
            XmlTextWriter myXmlWriter = new XmlTextWriter(myFileStream, Encoding.Unicode);
            ds.WriteXml( myXmlWriter,XmlWriteMode.WriteSchema );

      private DataTable GetTheData() {

            //get datatable
            DataTable dt = qs.GetDataTable("SELECT * FROM fin");
            DataTable d2 = dt.Copy();
            return d2;


      private DataTable DummyTable()       {
            //create table
            DataTable dt = new DataTable("Employees");

            //fill rows
            DataRow dr;
            for(int x=1;x<=10;x++) {
                  dr = dt.NewRow();
                  dr["id"] = x;
                  dr["FirstName"] = "Joe" + x;
                  dr["LastName"] = "Smith" + x;
                  dr["HireDate"] = DateTime.Now;

            return dt;

<!--#include file="qs.aspx" -->

jaiswalpragyaAuthor Commented:

Thanks for the reply.I am a little confused as to what this piece of code is doing.Inthe first reply u sent me, i saw the link.it says creating a pdf using crystal reports. I dont think this is what I want.I am not using any crystal reports and tuff.
I am creating a simple web form where the user can enter some values.It is a form of certain kind.Once those values are entered.I am displaying a page which has the data entered by the user.I want to creat a "Print" Button on that page, so that once the user hits the button a pdf file of the same form is created, for him to print it of.

I am not sure if this code does that.Could you please explain?

i've used abc.pdf befor it was easy to use and well doumented i have not used it though .net but im sure you could change the code below to work off .net they also offer a free licence if you link back to there site????  http://www.websupergoo.com/products.htm#pd they also have a .net ver and im sure some test code?

theID = 0
thePad = 10
theText = HTML

Set theDoc = Server.CreateObject("ABCpdf3.Doc")
theDoc.Width = 4
theDoc.FontSize = 32
theDoc.TextStyle.Justification = 1
theDoc.Rect.Inset 20, 20

theID = theDoc.AddImage("http://www.site.co.za/page.asp", 1)

  If theDoc.GetInfo(theID, "Truncated") <> "1" Then Exit Do
  theDoc.Page = theDoc.AddPage()
  theID = theDoc.AddImage(theID)

For i = 1 To theDoc.PageCount
  theDoc.PageNumber = i

tDir = Server.MapPath("pdfs/")
theDoc.Save tDir & "/temp_"& Session("User_ID") &"_"& Session("RndNumb") &".pdf"

Response.Redirect "pdfs/temp_"& Session("User_ID") &"_"& Session("RndNumb") &".pdf"


jaiswalpragyaAuthor Commented:
Thanks for the links.I will try these and see if it works.Also can you tell me if this link will be useful for my purpose?


I will be rateful if you can go through it and tell me if this would solve ym problem.


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now