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

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

Thanks.
0
jaiswalpragya
Asked:
jaiswalpragya
  • 2
  • 2
1 Solution
 
BrianGEFF719Commented:
Try This:

(http://www.net-language.com/CodeExample.aspx?i=655)


<%@ 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";

            //variables
            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();
                  ds.Tables.Add(dt);
                  CreateSchemaFile(xsdFile,ds);
                  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);
                  doc.Load(fileName);

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

                  doc.SetDataSource(ds);

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

                  doc.Export();
                  Response.Write(dt.Rows.Count);
                  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 );
            myXmlWriter.Close();
      }

      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");
            dt.Columns.Add("id",Type.GetType("System.Int32"));
            dt.Columns.Add("FirstName",Type.GetType("System.String"));
            dt.Columns.Add("LastName",Type.GetType("System.String"));
            dt.Columns.Add("HireDate",Type.GetType("System.DateTime"));

            //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;
                  dt.Rows.Add(dr);
            }

            return dt;
      }

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





0
 
BrianGEFF719Commented:
0
 
jaiswalpragyaAuthor Commented:
Hi,

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?

Thanks.
0
 
mk_bCommented:
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)

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

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

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

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

%>

../mk
0
 
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?

http://www.dotnetjunkies.com/Article/99C3464A-2CFE-4EE5-A74D-9DEE7F6F735D.dcik

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

Thanks.
0

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