Sunday, October 24, 2021

Export Crystal Reports to PDF in C#

When you want to export your Crystal Reports report to PDF format or any other format, this article provides guidance for exporting the Crystal Reports report to PDF format.


In this, we can use ExportOptions for exporting the Crystal Reports report to PDF format. In the following example, you can see how to export a Crystal Reports report as a PDF format file in C#. If you read my first article then it's easy to understand this example, if not then please read my first article.

Just add the following code to export a Crystal Reports report to PDF or other format.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

 

namespace CrystalReportDemo

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void btnShow_Click(object sender, EventArgs e)

        {

            ReportDocument cryRpt = new ReportDocument();

            cryRpt.Load(@"D:\C# Demos\Crystal Reports\CrystalReportDemo\CrystalReportDemo\CrystalReport1.rpt");

            crystalReportViewer1.ReportSource = cryRpt;

            crystalReportViewer1.Refresh();

            cryRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, @"D:\ASD.pdf");

            MessageBox.Show("Exported Successful");

        }

    }

}

In the code above we uesd the ReportDocument.ExportToDisk() function and in that the first parameter is the Export format type and the second parameter is the path to store the PDF file. Other options for exporting Crystal Reports to other formats are as follows:

exporting Crystal Reports

You see that the other format options include:
  1. Excel
  2. ExcelRecord
  3. ExcelWorkbook
  4. NoFormat
  5. HTML32
  6. HTML40
  7. PortableDocumentFormat
  8. RichText
  9. Text
  10. XML

These are some of the other formats for exporting Crystal Reports in C#.

When you run the application click the Show button, it will ask for a username and password then it will show you the result in ReportViewer and then one message will be displayed, PDF Exported.

Database Login
 
display PDF Exported

Here is your exported PDF:
 
Exported PDF

Conclusion

In this article we learned how to export a Crystal Reports report in PDF or other format. I hope this example will help you to learn the basics of a C# Crystal Reports application and exporting a Crystal Reports report to another format.

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...