Printing Tutorials

In software development, we often need to print/export reports on click event. Here is detail for printing/exporting report in both Dotnet and Visual Basic Development.
1. Printing Crystal Report in Dotnet
2. Exporting Crystal Report in Dotnet
3. Printing Data Report in VB
4. Exporting Data Report in VB

For More Detail in Crystal Report Click Here

1. Printing Crystal Report in Dotnet:
Crystal Report printing can be done in two different ways: first, by clicking print button in crystal report which appear in top left cortner. Second, programmatically by click event of button. Detail of second method is given below:-
a. Printing Crystal Report With Dialog Box:
ReportDocument rd = new ReportDocument();
rd.Load(@"E:\CryRpt\WindowsApplication1\WindowsApplication1\abc.rpt");
crystalReportViewer1.ReportSource = rd;
crystalReportViewer1.PrintReport();
b. Printing Crystal Report Without Dialog Box:
ReportDocument rd = new ReportDocument();
rd.Load(@"E:\CryRpt\WindowsApplication1\WindowsApplication1\abc.rpt");
crystalReportViewer1.ReportSource = rd;
crystalReportViewer1.Refresh();
rd.PrintToPrinter(1, false, 0, 0);
c. Printing Crystal Report Default Paper Setting:
PrintDialog PDialog = new PrintDialog();
PDialog.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
PDialog.PrinterSettings.DefaultPageSettings.Landscape = true;
PDialog.ShowDialog();
d. Printing Crystal Report Containing Print Dialog Box:
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
int ncopy = printDocument1.PrinterSettings.Copies;
int spage = printDocument1.PrinterSettings.FromPage;
int epage = printDocument1.PrinterSettings.ToPage;
string Printername = printDocument1.PrinterSettings.PrinterName;
ReportDocument rd = new ReportDocument();
rd.PrintOptions.PrinterName = Printername; rd.Load(@"E:\CryRpt\WindowsApplication1\WindowsApplication1\CrystalReport2.rpt");
rd.PrintToPrinter(ncopy, true, spage,epage);
}

2. Exporting Crystal Report in Dotnet:
Crystal Report exporting can be done in two different ways: first, by clicking export button in crystal report which appear in top left cortner. Second, programmatically by click event of button. Detail of second method is given below:-
a. Saving Crystal Report With Dialog Box:
ReportDocument rd = new ReportDocument();
rd.Load(@"E:\CryRpt\WindowsApplication1\WindowsApplication1\abc.rpt");
crystalReportViewer1.ReportSource = rd;
crystalReportViewer1.ExportReport();
Where "E:\CryRpt\WindowsApplication1\WindowsApplication1\abc.rpt" is path of the crystal report
b. Saving Crystal Report Without Dialog Box:
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load(@"E:\CryRpt\WindowsApplication1\WindowsApplication1\CrystalReport1.rpt");
crystalReportViewer1.ReportSource = cryRpt;
ExportOptions CrExportOptions;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
ExcelFormatOptions CrFormatTypeOptions;
CrDiskFileDestinationOptions.DiskFileName = @"c:\durgesh.pdf";
CrExportOptions = cryRpt.ExportOptions;
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.Excel;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
cryRpt.Export();

3. Printing Data Report in VB:
Printing a data report can be accomplished in one of two ways. The user can click the Print button that appears on the data report in Print Preview mode (using the Show method), or you can programmatically enable printing using the PrintReport method. If an error occurs during printing, trap it in the Error event.Detail of second method is given below:-
a. Printing Data Report With Dialog Box:
When printing a report programmatically, you have two choices: to print by displaying the Print dialog box, or by printing without displaying the dialog box.
To display the Print dialog box
1. Add a CommandButton to a Form.
2. In the button's Click event, place the following code:
3. DataReport1.PrintReport True
The Print dialog box allows the user to select a printer, print to file, select a range of pages to print, and specify the number of copies to print.
Note Printers must be installed on the computer in order to present a choice of printers.
b. Printing Data Report Without Dialog Box:
In some cases, you may wish to print the report without user intervention. The PrintReport method also gives you the option of selecting a range of pages to print, either all, or a specified range.
To print without displaying the dialog box
1. Add a CommandButton to a Form.
2. In the button's Click event, place the following code:
3. DataReport1.PrintReport False
Or, to specify a range of pages to print, use the code below:
DataReport1.PrintReport False, rptRangeFromTo, 1, 2

4. Exporting Data Report in VB:
After compiling a report you may wish to reuse it, either as part of a larger document or perhaps for distribution on an intranet or the Internet. The Data Report designer's ExportReport method allows you to accomplish these tasks. Using the ExportReport method, you can export any report as a text file or as an HTML file. Additionally, you can use any of a number of ExportFormat objects to tailor the content and appearance of an exported file.Detail is given below:-
a. Expoerting Data Report With Dialog Box:
By using one of the four members, you can export a report without creating another ExportFormat object, provided the default meets your requirements.
ObjectFile FilterDescription
ExportFormats(1) *.htm,*.html HTML
ExportFormats(2) *.htm,*.html Unicode HTML
ExportFormats(3) *.txt Text
ExportFormats(4) *.txt Unicode Text
If you need to use any of the default types, you can also use the Key property to specify a default type. The Key property values and the constants are shown below:
ObjectKey Constant
ExportFormats(1) key_def_HTML rptKeyHTML
ExportFormats(2) key_def_UnicodeHTML_UTF8 rptKeyUnicodeHTML_UTF8
ExportFormats(3) key_def_Text rptKeyText
ExportFormats(4) key_def_UnicodeText rptKeyUnicodeText
For Example: DataReport1.ExportReport rptKeyHTML
b. Expoerting Data Report Without Dialog Box:
The programmer can determine whether or not a dialog box will be presented when exporting a report. For example, if the report is created automatically every morning, and written to the same file for distribution by an intranet, there is no need to display a dialog box. As long as a valid file path and key are supplied, and the Overwrite parameter is set to True, the dialog will not be displayed.
For Example:
DataReport1.ExportReport rptKeyHTML, "C:\Temp\Daily_Report", True, , _rptRangeAllPages