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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.