Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » Crystal Reports C# » Tip: How to Print a Crystal Report Programmatically in ASP.NET?

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

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.

Author Rank :
Page Views : 130088
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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

 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
go to another page by kavita On October 26, 2006

 hi

I m kavita,My problem is L want to go another page when I click a row in crystal report

suppose I have 100 row in my report n when I click  a Row I want to go detail page is it possible

please help me

priya_pooja12324@yahoo.co.in

Reply | Email | Modify 
Cystal Report direct printing by akila On March 20, 2007
hi, Printtoprinter method is printing on serverside.But I want to print directly on client side printer.it is very urgent.Please help me.I need to print from web based POS BIll system. Thanks, Akila
Reply | Email | Modify 
Re: Cystal Report direct printing by captain On July 24, 2007

Hi,

 

I have to do the same thing. Please let me know if you solved your problem and got success in printing Crystal Report Client Side.

 

Thanks a lot in advance

Reply | Email | Modify 
Re: Cystal Report direct printing by riyaz On November 6, 2007
hi akila I have to do the same thing. Please let me know if you solved your problem and got success in printing Crystal Report Client Side thanks
Reply | Email | Modify 
doubt in crystal report by shylaja On March 22, 2007
how to connect from gridview to crystalreport directly thatmeans we are adding columns in gridview dynamically so with those columns we have to generate crystal report dynamically
Reply | Email | Modify 
Re: doubt in crystal report by Mahesh On March 22, 2007
I don't think you can connect a GridView with Crystal Report. You will have to add that column to your DataSet, which has data in it dynamically and pass that to the report.
Reply | Email | Modify 
How to print crytal reports from client side by suresh On April 12, 2007
hi i am suresh, i need to print crystal report from web page.
Reply | Email | Modify 
Re: How to print crytal reports from client side by Mahesh On April 13, 2007
Isn't this article shows how to print a report from a Web page?
Reply | Email | Modify 
Printing report from crystal report withouth print dialog by nupur On April 27, 2007
how will i do this for windows based application? Please provide me the solution i need it urgently
Reply | Email | Modify 
Re: Printing report from crystal report withouth print dialog by Mahesh On April 27, 2007
The code will be same for Windows application as well.
Reply | Email | Modify 
I want to set Paper Type eg. Fanfold 15 X 12 in for all report by Munish On May 14, 2007
Is it possible to set Paper Style in Crystal Report through AP.net. I want to set Paper Type eg. Fanfold 15 X 12 in for all report.
Reply | Email | Modify 
What are the required namespace? by dan On June 8, 2007
Hi Mr. Mahesh. May I know what are the namespace I need to include? Coz I have the following errors - 1. Dim DS As DataSet = GetData() -Name GetData is not declared 2. MyReport does not appear on the intellisense. Thanks
Reply | Email | Modify 
Re: What are the required namespace? by Mahesh On June 21, 2007

MyReport is the report you created as rpt file and added to your proejct.

GetData is my own method that returns the data from the database in a DataSet format. You have to write this method to get the data from the database.

Reply | Email | Modify 
Print directly to printer(Without Spooling) by shirish On August 1, 2007
Hi I am currently working on a VB.NET project with following requirements : Suppose I have to print .doc file having 10 pages using VB.Net program and after clicking on print button,suppose printer finishes printing 6 pages and then suddenly printer is out of papaer/light is off ,etc. then if I check the printer properties then it should not show remaining 4 pages in the queue. i.e. Generally what happens is that first 10 pages are taken into buffer of printer and then printing starts. But problem is that if printing operation stops due to some reason, then remaining pages will be in buffer. So I dont want that all pages should be in buffer.I want to print these pages one by one, so that if printing is stopped due to some reason then there should be no pages in queue. So how it can be done using vb.net programming
Reply | Email | Modify 
Print last page from Crystal report by partha On August 3, 2007
Hello experts.. Please help me.. I need to print last page of data using Crystal Report. Please reply me have any idea as soon as possible. Thank you
Reply | Email | Modify 
how to print crystalreport in web site by atiyeh On August 19, 2007
i want to print report with print Button in web site but when i click this button show java script error why? please help mi. thanks
Reply | Email | Modify 
Re: how to print crystalreport in web site by Antony On October 9, 2007

Hi....

on Clicking Print _ Btn

We can Open the Crystal Report in aPdf File Format and from there we can print the Crystal report

coding:

Dim Crpt As CrystalTestReport2

Dim MyTable As CrystalDecisions.CrystalReports.Engine.Table

Dim MyLogin As CrystalDecisions.Shared.TableLogOnInfo

 

Dim oStream As New System.IO.MemoryStream

Crpt = New CrystalTestReport2

Crpt.PrintOptions.PaperSize = PaperSize.PaperA4

Crpt.PrintOptions.PaperOrientation = PaperOrientation.Landscape

 

CrystalReportViewer1.ReportSource = Crpt

For Each MyTable In Crpt.Database.Tables

MyLogin = MyTable.LogOnInfo

MyLogin.ConnectionInfo.UserID = "sa"

MyLogin.ConnectionInfo.Password = ""

MyTable.ApplyLogOnInfo(MyLogin)

Next

 

 

Dim ReportDiskDestination As New CrystalDecisions.Shared.DiskFileDestinationOptions

Dim FileName As String = "c:\TestPDF\myreport2.pdf"

ReportDiskDestination.DiskFileName = FileName

With Crpt

.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile

.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat

.ExportOptions.DestinationOptions = ReportDiskDestination

.Export()

End With

Response.Redirect("c:\TestPDF\myreport2.pdf")

Reply | Email | Modify 
crystal report client side printing by riyaz On November 6, 2007
hi have u finish the client side printing successfully if means can u send the method to me??? i hope soon regards riyaz
Reply | Email | Modify 
Asp.net Crystal report , Load report failed error by Mehul On November 7, 2007
Hi Mahesh, I am getting error of Load report failed when I try to access crystal report using my asp.net application, can you give me one solution.
Reply | Email | Modify 
vs2005 c# ans cr with parameter by a On January 11, 2008
Hi , Using VS 2005 with integrated crystalreport On my computer is good developer parameters can be taken into consideration. But when I deploys on a client with clickonce I get an error on the reports with parameters Thanks
Reply | Email | Modify 
print the current value of a wenb page in crystal report by Shashank On October 22, 2008
hi, I m shashank, my problem is that I want to print current values of web page in the form of bill by using the crystal report and also want to add the same values in the database by clicking the single button please help me........
Reply | Email | Modify 
crystal report by sapna On June 1, 2009

 Hello All of You,

I am sapna,I have problem of print crystal report on server .when i Click on print option it is not tak any action

please help me

goldsapna@gamil.com

Reply | Email | Modify 
Re: crystal report by Mohammed On December 12, 2009

C# Code.
CrystalDecisions.CrystalReports.Engine.ReportDocument rptDoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
//_strFileName = Report Path
rptDoc.Load(_strFileName);

CrystalDecisions.Shared.TableLogOnInfo logOnInfo = null;

// Get the ConnectionInfo Object.
CrystalDecisions.CrystalReports.Engine.Tables  rptTable;
rptTable = rptDoc.Database.Tables;
for(int i=0 ; i < rptTable.Count ; i++)
{
 logOnInfo = new CrystalDecisions.Shared.TableLogOnInfo(); 
 logOnInfo = rptDoc.Database.Tables[i].LogOnInfo;
 ConnectionInfo connectionInfo = new ConnectionInfo();
 connectionInfo = logOnInfo.ConnectionInfo;

 // Set the Connection parameters.
 connectionInfo.DatabaseName = _strDBSName;
 connectionInfo.ServerName = _strDSN;
 connectionInfo.Password = _strUserID;
 connectionInfo.UserID = _strUserPWD;
 rptDoc.Database.Tables [i].ApplyLogOnInfo(logOnInfo);
}
PageMargins margins;
// Get the PageMargins structure and set the
// margins for the report.
margins = rptDoc.PrintOptions.PageMargins;
margins.bottomMargin = 350;
margins.leftMargin = 350;
margins.rightMargin = 350;
margins.topMargin = 350;
// Apply the page margins.
rptDoc.PrintOptions.ApplyPageMargins(margins);
rptDoc.PrintOptions.PrinterName = strPrinterName;
rptDoc.PrintToPrinter(_nCopies,false,0,0);

Reply | Email | Modify 
How to print a crystal report from client side? by Sri On June 25, 2009
Hi All,
Do you have any snippet code to print crystal report in client side?
Thanks.
Reply | Email | Modify 
Printer Paper Option by Aravind On April 5, 2010
Dear Sir,
             I have a doubt in print receipt, I created one application which shows data fully in crystal report and it print in A4 sheet. I dont want like that , i want to print data as like as a Medical Shop billing Paper. I think you may understand what i am trying to say.


Looking for your Anticipation reply

Thanks and Regards
Aravind G
g_aravind6@yahoo.com
Chennai
Reply | Email | Modify 
How to Print a Crystal Report Programmatically in ASP.NET? by Maulik On July 12, 2010
Hi,
I did this code to my project but it is working only on local machine it is not working on server.
Please, Help Me.

Thank You,
Maulik
Reply | Email | Modify 
Print Button Problem in Crystal Reports by Mani On July 23, 2010
i'm using crystal reports in Asp.Net with C#. But in this Print Button is not working.
Reply | Email | Modify 
Unable to take the PrinOut by Ravi On September 2, 2010
Hi,
I have an application which is developed on VS 2005, and I am able to take a Printout which is designed in Crystal Reports which comes with VS2005 Default. But when I host the same app on the Server, then I am unable to take the Printout. The Images on CR is showing Cross Marked. Please suggest me on the same.
Thanks in Advance.
RaviPola.
Reply | Email | Modify 
Printing crystal report by Zehan On December 11, 2010
Hi, I have a crystal report which i created for my POS system. Is there a way to automatically grow the size of the Details section of the report depending on the amount of rows that are going to be added to the report. It's really very urgent. Plz reply, Thanks in advance, Zehan
Reply | Email | Modify 
Error While Pinting by Anirban On February 10, 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
Reply | Email | Modify 
Print a Crystal Report Programmatically by brijendra On September 27, 2011
report.PrintToPrinter(1, False, 0, 0) this code doesn't work while i deploy the application on iis server.. please help
Reply | Email | Modify 
Re: Print a Crystal Report Programmatically by Mani On September 27, 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.
Reply | Email | Modify 
Print a Crystal Report Programmatically in ASP.NET by brijendra On September 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?
Reply | Email | Modify 
Re: Print a Crystal Report Programmatically in ASP.NET by Mani On September 28, 2011
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.
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.