Muhammad Atif Agha
Muhammad Atif Agha

Reputation: 1545

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.

Upvotes: 9

Views: 39293

Answers (2)

AjayR
AjayR

Reputation: 4179

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.

Upvotes: 5

IAmTimCorey
IAmTimCorey

Reputation: 16757

Here is the solution you are looking for:

http://www.c-sharpcorner.com/UploadFile/mahesh/ExportCRtoPDF10062006161918PM/ExportCRtoPDF.aspx

Here is the quote from the site:

The following steps will guide you to achieve the same:

Add crystal report (.cr) file to your ASP.NET application. Add a report instance on the page level.

Dim report As MyReport = New MyReport

Populate reports data on Page_Init

Dim ds As DataSet = GetData()

report.SetDataSource(ds)

Export Report

report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, False, "ExportedReport")

If you wish to format report to other formats, just change the ExportFormatType enumeration value to > your desired format.

If you wish to download the report, then you simply change the third parameter of >ExportToHttpResponse method in Step 4 to True.

Upvotes: 3

Related Questions