Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crystal Report direct saving as PDF, instead of viewing

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.

like image 698
Muhammad Atif Agha Avatar asked Jun 02 '11 05:06

Muhammad Atif Agha


People also ask

How do I save a Crystal Report?

In Crystal Reports Viewer, in the toolbar, click the Export Report button. In the Export Report window, navigate to the folder on your computer where you want to save your report. In the File name field, enter a name for the file you are exporting. In the Save as type dropdown list, select a file type.


1 Answers

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.

like image 123
AjayR Avatar answered Oct 16 '22 00:10

AjayR