VB.NET Crystal Reports Export to PDF

When exporting from Crystal Reports to the PDF format, we utilize the CrExportOptions class provided by Crystal Reports. Additionally, we need to configure the PdfRtfWordFormatOptions and set the ExportFormatType to PortableDocFormat. The following example demonstrates the process of exporting a Crystal Report as a PDF file.

Before investigating into the Crystal Reports programming samples provided in this tutorial, it is recommended to familiarize yourself with the database structure that serves as the foundation for these examples. The database used in these tutorials is referred to as "crystaldb." To ensure a comprehensive understanding of the programming samples, we encourage you to review the database structure beforehand. You can access the database structure by clicking on the provided link: "Click here to see Database Structure." By acquainting yourself with the database structure, you will be better equipped to follow along with the tutorial and comprehend the various programming concepts demonstrated. - Click here to see Database Structure .

In this tutorial, we will be utilizing a previously developed program that provides a step-by-step guide on creating Crystal Reports to extract data from a database and present it in the reports. Prior to commencing this section, it is advisable to review the comprehensive step by step Crystal Report guide in VB.NET.

By referring to the step by step Crystal Report tutorial, you will gain a solid understanding of the systematic process involved in retrieving data from a database and effectively showcasing it using Crystal Reports. Familiarizing yourself with the concepts covered in the earlier Crystal Report program will significantly enhance your ability to grasp and implement the concepts discussed in the subsequent sections of this tutorial.

In the step-by-step Crystal Report guide, we learned how to retrieve all data from the Product table. In this section, we will focus on exporting that retrieved data to a PDF format file.

Select the default form (Form1.vb) you created in VB.NET and drag two buttons (Button1, Button2 ) and CrystalReportViewer control to your form.

Select Form's source code view and import the following :

Imports CrystalDecisions.CrystalReports.Engine

Put the following source code in the button click events

Full Source VB.NET
Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Public Class Form1 Dim cryRpt As New ReportDocument Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") CrystalReportViewer1.ReportSource = cryRpt CrystalReportViewer1.Refresh() End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Try Dim CrExportOptions As ExportOptions Dim CrDiskFileDestinationOptions As New _ DiskFileDestinationOptions() Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions() CrDiskFileDestinationOptions.DiskFileName = _ "c:\crystalExport.pdf" CrExportOptions = cryRpt.ExportOptions With CrExportOptions .ExportDestinationType = ExportDestinationType.DiskFile .ExportFormatType = ExportFormatType.PortableDocFormat .DestinationOptions = CrDiskFileDestinationOptions .FormatOptions = CrFormatTypeOptions End With cryRpt.Export() Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class