Exporting to PDF, RTF, and Word Documents
The process for exporting to PDF, RTF, and MS Word shares the same classes for all three. In fact, the code is identical except for the property that sets the export format type, which specifies which of the three types to use.
Exporting a report to PDF, RTF or Word gives you the option to specify the page range. You can set the starting and ending page or simply print out the entire report. Note that all pages must be consecutively numbered. For example, you can’t export the first few pages of a report and the last few pages of a report. Table 19-4 shows the properties used for setting the page range.
Table 19-4. PdfRtfWordFormatOptions Properties.
Property | Description |
---|---|
UsePageRang | Boolean that enables/disables the use of page ranges |
FirstPageNumbe | The first page number to export |
LastPageNumbe | The last page number to export |
The following listing first creates an ExportOptions object variable, myExportOptions, and tells it that the export file will be saved to a disk file using the DiskFileDestinationOptions object.
After that it sets the format type to PDF. If you want to export to RTF or Word, change this line to be the appropriate enumeration. After setting the format type, create a format options object and set the page range. If all the pages are going to be exported, set the UsePageRange property to False. Otherwise, set it to True and also set the FirstPageNumber and LastPageNumber properties.
Finally, assign the format options object to the myExportOptions object and call the ReportDocument.Export() method.
The following code listings assume that you already instantiated a ReportDocument object and loaded the report into memory. This report object is one of the parameters listed in the method declaration. The other parameters specify the values for the exporting process (filename, which pages to print, etc.). These parameters are self-explanatory.
Listing 19-1. Set the export formatting to PDF
[VB.NET]
Public Sub ExportPdfRtfWord(ByVal Filename As String, ByRef myReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal UsePageRange As Boolean, ByVal FirstPageNumber As Integer, ByVal LastPageNumber As Integer)
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
'Set the export to a disk file
myExportOptions.ExportDestinationType = _
CrystalDecisions.Shared.ExportDestinationType.DiskFile
Dim myDestinationOptions As CrystalDecisions.Shared.DiskFileDestinationOptions
myDestinationOptions = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions
myDestinationOptions.DiskFileName = Filename
myExportOptions.ExportDestinationOptions = myDestinationOptions
'Change the next line if you want the format to be RTF or Word
myExportOptions.ExportFormatType = _
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
'The following lines stay the same regardless of formatting
Dim myFormatOptions As CrystalDecisions.Shared.PdfRtfWordFormatOptions
myFormatOptions = CrystalDecisions.Shared.ExportOptions.CreatePdfRtfWordFormatOptions()
myFormatOptions.UsePageRange = UsePageRange
If UsePageRange Then
myFormatOptions.FirstPageNumber = FirstPageNumber
myFormatOptions.LastPageNumber = LastPageNumber
End If
myExportOptions.FormatOptions = myFormatOptions
myReport.Export(myExportOptions)
End Sub
[C#]
public void ExportPdfRtfWord(string Filename, ref CrystalDecisions.CrystalReports.Engine.ReportDocument myReport, bool UsePageRange, int FirstPageNumber, int LastPageNumber)
{
CrystalDecisions.Shared.ExportOptions myExportOptions = new CrystalDecisions.Shared.ExportOptions();
//Set the export to a disk file
myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
CrystalDecisions.Shared.DiskFileDestinationOptions myDestinationOptions;
myDestinationOptions = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions();
myDestinationOptions.DiskFileName = Filename;
myExportOptions.ExportDestinationOptions = myDestinationOptions;
//Change the next line if you want the format to be RTF or Word
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
//The following lines stay the same regardless of formatting
CrystalDecisions.Shared.PdfRtfWordFormatOptions myFormatOptions;
myFormatOptions = CrystalDecisions.Shared.ExportOptions.CreatePdfRtfWordFormatOptions();
myFormatOptions.UsePageRange = UsePageRange;
if (UsePageRange) {
myFormatOptions.FirstPageNumber = FirstPageNumber;
myFormatOptions.LastPageNumber = LastPageNumber;
}
myExportOptions.FormatOptions = myFormatOptions;
myReport.Export(myExportOptions);
}
Question: I need to export multiple reports into a single PDF file. How do I merge them together into a single file?
Answer: Unfortunately, Crystal Reports can’t do this. One workaround is to create a blank main report and add all the reports you want to merge as subreports. That lets them print out consecutively and you can export this new report to PDF. The other way is to download a third-party Crystal Reports/PDF tool. I haven’t used one myself, but I see them advertised in Google search results.