Introduction
Crystal Reports is a popular business intelligence reporting tool that enables developers to create, design, and deliver reports. Crystal Reports Viewer is a standalone application used to view and interact with Crystal Reports. In some cases, you might need to set custom paper sizes when printing reports using Crystal Reports Viewer in C#. This article provides a comprehensive guide on how to achieve that.
Prerequisites
Before we dive into the solution, ensure you have the following prerequisites:
- Visual Studio with Crystal Reports and Crystal Reports Viewer components installed.
- A Crystal Report file.
Step 1: Create a New Crystal Report Viewer Project
First, create a new Crystal Report Viewer project in Visual Studio. To do this:
- Open Visual Studio.
- Click on "Create new project" and select "Windows Forms App (.NET)."
- Name the project and click "Create."
- Install the Crystal Reports and Crystal Reports Viewer components from the "Tools" menu.
Step 2: Design the Form
Design the form by adding a Crystal Report Viewer component to it:
- Drag and drop a CrystalReportViewer component from the Toolbox onto the form.
Step 3: Load the Crystal Report
Load the Crystal Report into the Crystal Report Viewer:
using CrystalDecisions.CrystalReports.Engine;
// ...
ReportDocument report = new ReportDocument();
report.Load(@"C:\path\to\your\report.rpt");
crystalReportViewer1.ReportSource = report;
Step 4: Set Custom Paper Size
Set the custom paper size when printing the report:
using CrystalDecisions.Shared;
// ...
report.PrintOptions.PaperSize = new PaperSize(9, 6); // Set custom paper size (9x6 inches)
report.PrintOptions.PrinterName = "Your Printer Name"; // Set the printer name
report.PrintToPrinter(1, false, 0, 0); // Print the report
In this article, we learned how to set custom paper sizes when printing Crystal Reports using Crystal Reports Viewer in C#. By following the steps outlined above, you should be able to customize the paper size according to your requirements.