Resources » Code Snippets » Crystal Reports

How To Export Crystal Report to PDF in ASP.Net


Last Updated:   Category: Crystal Reports    
Author: Member Level: Bronze    Points: 7



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


Did you like this resource? Share it with your friends and show your love!

Responses to "How To Export Crystal Report to PDF in ASP.Net"
Guest Author: binh michel     12 May 2013
How to send PDF report to client machine using ExportCrystalReportToPDF ? Please help me?


Feedbacks      

Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    umesh
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India