How to Export Crystal Report to PDF in C#, ASP.NET

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

Crystal report Export To PDF
Crystal report Export 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.

Add Command buttons

Once you have created the project do the following

  1. Select CrystalReportViewerControl.
  2. Go to properties
  3. Locate Dock Property
    The default value of the dock property is Fill. The Fill property will not allow you to resize the control.
  4. Select it as None
  5. Resize CrystalReportViewerControl.
    Make free space at the top of the form.
  6. Add a command button. Name it as ExporttoDiskBtn and caption ExporttoDisk
  7. Add another command button. Name it as ExportBtn and caption Export.The form will look like this
Crystal Report Export To PDF
Add Command Buttons as shown

Open the form and import the following namespaces.

If you have installed 32bit version of crystal report, then go and change platform target using the following steps

Export To PDF platform Target
Export To PDF platform Target
  1. Select Project.
  2. Right Click.
  3. Select Properties.
  4. Select Build.
  5. Select Platform target.
  6. 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

The code will look like this

ExporttoDisk
ExporttoDisk

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

Code Explanation

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.

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 .

Finally, Create a variable ex to hold the ExportOptions.

Assign ExportDestinationType  to DiskFile as we are saving to Hard Disk.

Assign ExportDestinationOptions  to dest  variable, which is of type diskfiledestinationOption

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

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 thought on “How to Export Crystal Report to PDF in C#, ASP.NET”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top