C#•Generate PDFs Dynamically

Listing 4. You can use a Crystal Report as the source for generating an Adobe Acrobat PDF file dynamically. Utilize the Export() method of the ReportDocument class to save the results in PDF format (or Word or Excel) to a local temporary file. Then stream the contents of the file back to the browser for display to the user.

using System.IO;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
public class OrdersPDFExport : System.Web.UI.Page
   {
      protected System.Data.SqlClient.SqlCommand 
         sqlSelectCommand1;
      protected System.Data.SqlClient.SqlConnection 
         sqlConnection1;
      protected System.Data.SqlClient.SqlDataAdapter 
         sqlDataAdapter1;
      protected VSM102004.OrderDetailsForOrder 
         dsReportOrderDetails;
      protected SqlParameter ParamOrderID;
      private void Page_Load(object sender, 
         System.EventArgs e)
      {
         ExportOptions myExportOptions;
         DiskFileDestinationOptions 
            myDiskFileOptions;
         String strTempFileLocation;
         ReportDocument myReportDocument;
         // Make sure an OrderID was sessioned off
         if (Session["OrderID"] != null)
         {
            // Set stored proc filter value to select only order 
            // data for this Order ID
            ParamOrderID.Value = 
               Session["OrderID"].ToString();
            // Get data for order and populate dataset
            sqlDataAdapter1.Fill
               (dsReportOrderDetails);
            // Set up new report based on Crystal RPT class created
            myReportDocument = new ReportOrders();
            myReportDocument.SetDataSource
               (dsReportOrderDetails);
            // Set temporary location for PDF export
            strTempFileLocation = Session.
               SessionID.ToString() + ".pdf";
            // Now set up export options
            myDiskFileOptions = new 
               DiskFileDestinationOptions();
            myDiskFileOptions.DiskFileName = 
               Server.MapPath(strTempFileLocation);
         myExportOptions = 
            myReportDocument.ExportOptions;
         myExportOptions.DestinationOptions = 
            myDiskFileOptions;
         myExportOptions.ExportDestinationType =
            ExportDestinationType.DiskFile;
         myExportOptions.ExportFormatType =
            ExportFormatType.PortableDocFormat;
         // Now export to PDF file
         myReportDocument.Export();
         // Now stuff PDF file into rendering stream
         // First, clear everything else dynamically created
         // and just send PDF file
         Response.ClearContent();
         Response.ClearHeaders();
         Response.ContentType = "application/pdf";
         Response.WriteFile(Server.MapPath
            (strTempFileLocation));
         Response.Flush();
         Response.Close();
         // Now you can clean up temporary PDF export file
         File.Delete(Server.MapPath
            (strTempFileLocation));
      }
   }