How To Export Crystal Report to PDF in ASP.Net

Assuming that you have already bound the ReportDocument with some data source e.g.



someReportDocument.SetDataSource(someDataTable);


now after binding all you need is to pass that report document to the following function.


private void ExportCrystalReportToPDF(ReportDocument doc)
{
try
{
ExportOptions CrExportOptions;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
CrDiskFileDestinationOptions.DiskFileName = "" + Path.GetTempPath() + @"\Report.pdf";
CrExportOptions = doc.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
doc.Export();
}
catch (Exception ex)
{
DisplayErrorMsg(ex.ToString());
}
}


if you use this methodology of exporting a crystal report to pdf, the resulting file will be saved on server side. later you can email this pdf report to your client.

Another way of exporting crystal report to pdf is to directly convert Report Document to Stream. After that transmit this stream to client along with the response object.


// include namespace System.IO;
using System.IO;

private void ExportCrystalReportToPDF(ReportDocument ReportDoc)
{
System.IO.MemoryStream s = (MemoryStream)ReportDoc.ExportToStream(ExportFormatType.PortableDocFormat);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + "Report.pdf");
HttpContext.Current.Response.BinaryWrite(s.ToArray());
HttpContext.Current.Response.End();
}


the advantage of second exporting methodology is that the resulted pdf report can directly be saved to client's Machine (i.e no server side storage required)
well that's all

let me know if you might have some problems
happy surfing

Regards
Aneela


Comments

Guest Author: binh michel12 May 2013

How to send PDF report to client machine using ExportCrystalReportToPDF ? Please help me?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: