Crystal Report direct saving as PDF, instead of viewing

Muhammad Atif Agha picture Muhammad Atif Agha · Jun 2, 2011 · Viewed 36.6k times · Source

I want to make a report from ASP.Net, in Crystal Report. I want, when user click on print, it should simply show a browser dialog of Save,Open,Save as, and PDF should be saved, or Crystal Report print preview should appear, I don't want to display report first in Viewer then click on button to get print or PDF, I want simply from clicking on asp button, I have all the idea of parameters and know how to make report, my question is just to not to show viewer and take report from asp button in a form of PDF or print preview dialog to print. I have used the Export method of .Net for Crystal Report, but it does not work.

Answer

AjayR picture AjayR · Jun 2, 2011

You can generate a PDF by Using a Crystal Report and piece of code....

  • First: Generate a Crystal Report as per your requirements.

  • Second: Use the below code to generate the PDF:

    • Place the following name spaces at the top of the code page

      Imports CrystalDecisions.CrystalReports.Engine
      Imports CrystalDecisions.Shared
      
    • Variable Declaration

      Dim CrReport As New CrystalReport1() // Report Name
      Dim CrExportOptions As ExportOptions
      Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
      Dim CrFormatTypeOptions as New PdfRtfWordFormatOptions()
      
    • Set the destination path and file name

      CrDiskFileDestinationOptions.DiskFileName = "c:\RichText.pdf"
      
    • Specify a page range (optional)

      crFormatTypeOptions.FirstPageNumber = 1 // Start Page in the Report
      crFormatTypeOptions.LastPageNumber = 3 // End Page in the Report
      crFormatTypeOptions.UsePageRange = True
      
    • Set export options

      CrExportOptions = crReport.ExportOptions
      
      With CrExportOptions
      
      // Set the destination to a disk file
      .ExportDestinationType = ExportDestinationType.DiskFile
      
      // Set the format to PDF
      .ExportFormatType = ExportFormatType.PortableDocFormat
      
      // Set the destination options to DiskFileDestinationOptions object
      .DestinationOptions = CrDiskFileDestinationOptions
      .FormatOptions = crFormatTypeOptions
      
      End With
      
    • Trap any errors that occur on export

      Try
          // Export the report
          CrReport.Export()
      Catch err As Exception
          MessageBox.Show(err.ToString())
      End Try
      

Thats it.... Now you are ready to create a PDF of the Report.