Tuesday, June 12, 2012

Asp.Net Export to PDF using Crystal Reports


In this post Asp.Net Export to PDF using Crystal Reports, we shall see on how to export the content of a Crystal report to a PDF file. 

Here we will use the report which we designed in the port Crystal Reports CrossTab Report in Asp.Net. To know more on the basics of how to create a Cross-tab report refer the post 
Crystal Reports CrossTab Report in Asp.Net.

All the procedures for designing the report, binding the data to the report will remain same as we saw in the post Crystal Reports CrossTab Report in Asp.Net. Here instead of displaying the report in the CrystalReportViewer, we will export the report as a PDF file.

Add the following lines of code before and after binding data to the CrystalReport

Before
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=Report.pdf");
HttpContext.Current.Response.ContentType = "application/pdf";
MemoryStream objStream;

After
objStream = (MemoryStream)objRptCrossTab.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(objStream.ToArray());


The complete code is as follows

HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=Report.pdf");
HttpContext.Current.Response.ContentType = "application/pdf";
MemoryStream objStream;

// Add Data access code to get the data into the Dataset dsCrossTab
objRptCrossTab = new rptCrossTab();
objRptCrossTab.SetDataSource(dsCrossTab);

// Export the Report to PDF
objStream = (MemoryStream)objRptCrossTab.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
//
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(objStream.ToArray());

Save and run the page, the File Download dialog will pop-up, you can save the PDF file to the disk by selecting the required location.

That's it we have exported a Crystal Report to PDF file using Asp.Net

Search Flipkart Products:
Flipkart.com

3 comments:

Narayan said...

Hi Prakash,

Can u please add the entire source code as zip file.
Giving error at
"rptCrossTab objRptCrossTab;"

Prakash B said...

Hi Narayan,

We cannot upload code in the Blogger.

In this like
rptCrossTab objRptCrossTab
We are creating an instance of the report file.

You need to create a repeate Crystal Report file with the name rptCrossTab, or your own report name.

Refer the post Crystal Reports CrossTab Report in Asp.Net, to see on how to create the Report.

lingmaaki said...

ExportOptions CrExportOptions ;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
CrDiskFileDestinationOptions.DiskFileName = "c:\\csharp.net-informations.pdf";
CrExportOptions = cryRpt.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
cryRpt.Export();


http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-export-pdf.htm

ling