There are times when you would like to Export Crystal reports to various formats like PDF, Excel, etc. We have a tutorial which explains the Various Report formats support by Crystal Reports. We have also explained how to Export Crystal Report Using Crystal Report Viewer Control and also explained the SDK Function available to Export.
In this tutorial, we will show how to export Crystal Report to PDF using an example.
How to Export Crystal Report to PDF
Create Crystal Report
Create a Visual Studio Project and add Crystal Report to it. If you are new to Crystal Report then read How to Create Crystal Report in Visual Studio from this link.
In that project, we had used an employee table to generate the report. In this tutorial let us Customer table and create a report CustomerList. I will leave the report creation part to you and in case if you have any issues, please post your problem in the comments section.
Once you have created the project do the following
- Select CrystalReportViewerControl.
- Go to properties
- Locate Dock Property
The default value of the dock property is Fill. The Fill property will not allow you to resize the control. - Select it as None
- Resize CrystalReportViewerControl.
Make free space at the top of the form. - Add a command button. Name it as ExporttoDiskBtn and caption ExporttoDisk
- Add another command button. Name it as ExportBtn and caption Export.The form will look like this
Open the form and import the following namespaces.
1 2 3 4 | using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; |
If you have installed 32bit version of crystal report, then go and change platform target using the following steps
- Select Project.
- Right Click.
- Select Properties.
- Select Build.
- Select Platform target.
- Select x86.
ExportToDisk
ExportToDisk is the simplest of export command. It takes 2 parameters. Export format type and File Name.
Double click on ExporttoDisk button and type the following code
1 2 3 4 5 6 7 8 9 10 | private void ExporttoDiskBtn_Click(object sender, EventArgs e) { CustomerList CrReport = new CustomerList(); CrReport.ExportToDisk(ExportFormatType.PortableDocFormat,"C:\CustomerList.pdf"); MessageBox.Show("Exported"); } |
The code will look like this
Run the project and click on ExportToDiskBtn and you will report is exported to C:\CustomerList.pdf
Export function
Another way to export is to use the export function. This function gives more flexibility than the exportToDisk function.
Double click on Export Button and type the following Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | private void ExportBtn_Click(object sender, EventArgs e) { CustomerList CrReport = new CustomerList(); DiskFileDestinationOptions dest = new DiskFileDestinationOptions(); dest.DiskFileName = "C:\\CustomerList.pdf"; PdfFormatOptions formatOpt = new PdfFormatOptions(); formatOpt.FirstPageNumber=0; formatOpt.LastPageNumber = 0; formatOpt.UsePageRange = false; formatOpt.CreateBookmarksFromGroupTree = false; ExportOptions ex = new ExportOptions(); ex.ExportDestinationType = ExportDestinationType.DiskFile; ex.ExportDestinationOptions = dest; ex.ExportFormatType=ExportFormatType.PortableDocFormat; ex.ExportFormatOptions = formatOpt; CrReport.Export(ex); MessageBox.Show("Exported"); } |
Code Explanation
1 2 3 4 | DiskFileDestinationOptions dest = new DiskFileDestinationOptions(); dest.DiskFileName = "C:\\CustomerList.pdf"; |
The above code creates dest variable of type DiskFileDestinationOptions, which means that the destination of export is a disk file. Other DestinationOptions are Microsoft Mail and exchangefolder. DiskFileDestinationOptions has one property DiskFileName. Assign the name of the file to this property.
1 2 3 4 5 6 | PdfFormatOptions formatOpt = new PdfFormatOptions(); formatOpt.FirstPageNumber=0; formatOpt.LastPageNumber = 0; formatOpt.UsePageRange = false; formatOpt.CreateBookmarksFromGroupTree = false; |
The above code creates formatOpt variable of type PdfFormatOptions, which means that the format of the export file is a PDF file. Each Export Types (Excel, Word, CSV) in Crystal Report has their own formatOptions class.
PdfFormatOptions has four Properties. FirstPageNumber, LastPageNumber, UsePageRange, and CreateBookmarksFromGroupTree .
1 2 3 | ExportOptions ex = new ExportOptions(); |
Finally, Create a variable ex to hold the ExportOptions.
1 2 3 | ex.ExportDestinationType = ExportDestinationType.DiskFile; |
Assign ExportDestinationType to DiskFile as we are saving to Hard Disk.
1 2 3 | ex.ExportDestinationOptions = dest; |
Assign ExportDestinationOptions to dest variable, which is of type diskfiledestinationOption
1 2 3 | ex.ExportFormatType=ExportFormatType.PortableDocFormat; ex.ExportFormatOptions = formatOpt; |
Similarly, ExportFormatType and ExportFormatOptions are assigned to corresponding variables.
Run the project, click on Export button and you will see that the report is exported to C:\CustomerList.pdf
Export Page Range
Make UsePageRange=true and assign Staring page number and Last Page number to FirstPageNumber and LastPageNumber
1 2 3 | formatOpt.UsePageRange = true; formatOpt.FirstPageNumber=1; formatOpt.LastPageNumber =1; |
Create Bookmark in PDF
If the report is grouped by a field, then you can add the bookmark in the exported PDF by using CreateBookmarksFromGroupTree property.
1 2 3 | formatOpt.CreateBookmarksFromGroupTree = true; |
this is very good article