Geeks With Blogs
Neil Smith [blog]code til ur fingers bleed

Dynamically having web sites generate PDF documents is quite a common requirement, I’ve had to do it for order forms, invoices, etc. There are a few third party components that can make the job easier, though I’ve found they tend to be quite expensive to buy, take for example http://www.xmlpdf.com/ibex.html

 

The good news is that as of crystal reports 8.5, PDF is one of the export options that covers the versions of crystal that ship with visual studio 2003 and 2005 editions.  Using the crystal web viewer control which will take the file INV.RPT which will get its data from INV.XML and then generate an INV.PDF and with a little javascript to open a window showing the generated PDF.  Simply design your PDF’s from within the Crystal Reports IDE… here’s the code…

 

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

using System.IO;

using System.Xml;

using System.Text;

 

private void pdfCreate(string action)

{

      //*** 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 = "inv";

      string rptFile = "inv.rpt";

      string xsdFile = "inv.xml";

      string pdfFile = "inv.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("inv.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 = GetDataTable("SELECT * FROM inv");

      DataTable d2 = dt.Copy();

      return d2;

 

}

private DataTable GetDataTable(string sSQL)

{

      DataTable dt = new DataTable();

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

 

      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;

}

 

private DataTable DummyTable()     

{

      //create table

      DataTable dt = new DataTable("NSClient");

      dt.Columns.Add("id",Type.GetType("System.Int32"));

      dt.Columns.Add("Name",Type.GetType("System.String"));

      dt.Columns.Add("Company",Type.GetType("System.String"));

      //dt.Columns.Add("HireDate",Type.GetType("System.DateTime"));

 

      //fill rows

      DataRow dr;

      for(int x=1;x<=1;x++)

      {

            dr = dt.NewRow();

            dr["id"] = x;

            dr["Name"] = txtfname.Text;

            dr["Company"] = txtcompany.Text;

            dt.Rows.Add(dr);

      }

 

      return dt;

}

 

private void Submit1_ServerClick(object sender, System.EventArgs e)

{

      pdfCreate("CreateSchemaFile");

      pdfCreate("CreatePdf");

      //Response.Redirect("inv.pdf");          

      string popupScript = string.Concat("<script language='javascript'> window.open('inv.pdf', 'Report', 'width=790, height=500, menubar=no, scrollbars=yes, resizable=yes')</script>");

 

      Page.RegisterStartupScript("PopupScript", popupScript);

 

}

 

Posted on Tuesday, June 13, 2006 6:56 AM | Back to top


Comments on this post: More adventures in code..

# re: More adventures in code..
Requesting Gravatar...
BTW, if it's not too much trouble, can you comment the whole code block?

Mahesh
Left by Mahesh on Jun 13, 2006 5:44 PM

# re: More adventures in code..
Requesting Gravatar...
I am not sure what happened to my earlier comment, so here I go again.

My feedreader program is trying to execute the code in your blog entry. It keeps on crashing whenever it does that.

If it's not too much trouble, can you comment the whole code block?
Left by Mahesh on Jun 13, 2006 5:48 PM

Your comment:
 (will show your gravatar)


Copyright © Neil Smith | Powered by: GeeksWithBlogs.net