ARTICLE

Tip: How to Print a Crystal Report Programmatically in ASP.NET?

Posted by Mahesh Chand Articles | Crystal Reports C# October 16, 2006
You can print a Crystal Report using print option of Crystal Report Viewer. However, there are occasions when you want your application to print a report direct to printer without viewing the report in Crystal Report Viewer.
Reader Level:

You can print a Crystal Report using print option of Crystal Report Viewer. However, there are occasions when you want your application to print a report direct to printer without viewing the report in Crystal Report Viewer.

The ReportDocument class provides PrintToPrinter method that may be used to print a CR direct to the printer. If no printer is selected, the default printer will be used to send the printing pages to.

 

The PrintToPrinter method takes four parameters.

 

nCopies : Indicates the number of copies to print.

collated : Indicates whether to collate the pages.

startPageN : Indicates the first page to print.

endPageN : Indicates the last page to print.

 

The following steps will guide you to achieve the same:

 

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

    Dim report As MyReport = New MyReport

 

  1. Populate reports data on Page_Init 

     
    ' Get data in a DataSet or DataTable

        Dim ds As DataSet = GetData()

        ' Fill report with the data

     report.SetDataSource(ds)

 

  1. Print Report
    report.PrintToPrinter(1, False, 0, 0)

 

If you wish to print certain page range, change last two parameters From to To page number.

 

If you want to set page margins, you need to create a PageMargin object and set PrintOptions of the ReportDocument.

 

The following code sets page margins and printer name:

 

Dim margins As PageMargins =  Report.PrintOptions.PageMargins

   margins.bottomMargin = 200

   margins.leftMargin = 200

   margins.rightMargin = 50

   margins.topMargin = 100

   Report.PrintOptions.ApplyPageMargins(margins)

 

   ' Select the printer name

   Report.PrintOptions.PrinterName = printerName

 

Login to add your contents and source code to this article
post comment
     

Hi, Sometimes when u hosting on server the Print button won't works because of DLL problem. So you need to add Script Manager.If you send that code means i'll say exact problem.

Posted by Mani maran Sep 28, 2011

hi mani, report.PrintToPrinter(1, False, 0, 0) this code doesn't work on server its works fine on localhost... and what is the purpose to add script manager?

Posted by brijendra sharma Sep 28, 2011

Hi.. Can u send ur code to my mail(send2mm@gmail.com)..... if not means add script manager to ur code.. that will works fine.

Posted by Mani maran Sep 27, 2011

report.PrintToPrinter(1, False, 0, 0) this code doesn't work while i deploy the application on iis server.. please help

Posted by brijendra sharma Sep 27, 2011

Hi Mahesh, I am trying to print a Crystal report thru a WCF service. When I am running the WCF service in a stand alone mode the Crstal report is getting printed correctly. Now when a client is trying to call that service [which in turn prints the report], is giving the following error : "RPC server not available". Stach Trace : at System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument document, PrintEventArgs e) at System.Drawing.Printing.PrintController.Print(PrintDocument document) at System.Drawing.Printing.PrintDocument.Print() at CrystalDecisions.CrystalReports.Engine.FormatEngine.PrintToPrinter(Int32 nCopies, Boolean collated, Int32 startPageN, Int32 endPageN) at CrystalDecisions.CrystalReports.Engine.ReportDocument.PrintToPrinter(Int32 nCopies, Boolean collated, Int32 startPageN, Int32 endPageN) at CISPrintService.PrintService.DoBatchPrint(PrintDC PobjPrintDC) in C:\Inetpub\wwwroot\CISPrintService\PrintService.svc.cs:line 91 I have checked all the following service is up & running: 1)Printer Spooler 2) RPC 3) RPC Locator Could you please help me in this regard? Thanks, Anirban

Posted by Anirban Kundu Feb 10, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.