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

Posted on 2004-11-28
Last Modified: 2012-08-13
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.

Question by:jaiswalpragya
    LVL 19

    Expert Comment

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

    LVL 19

    Expert Comment


    Author Comment


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

    LVL 5

    Accepted Solution

    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???? 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("", 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"



    Author Comment

    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

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video discusses moving either the default database or any database to a new volume.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now