Automatically Printing Crystal Reports in ASP.NET

xiaoxiao2021-03-06  117

Introduction Unlike the Windows Forms CrystalReportViewer toolbar, the web-based CrystalReportViewer toolbar does not include a print button. For reports that are directly rendered as HTML, the user can simply use the browser's print button. For reports rendered as PDF or Excel, the user can use the print functionality in either the Acrobat Reader or Excel to print the report. Most users can figure out how to print the report, but some still need a little help, and in some cases, clients request this as part of their application specs . In this article, we'll review two methods for automatically printing a Crystal Report using the web-based CrystalReportViewer: the server side ReportDoucment.PrintToPrinter method, and a small client-side JavaScript that uses the window.print method We'll. Also Discuss Rendering A Report In Pdf and Alload The User To Print That Document As An Alternative To The Two Other Methods. Because of the Disconnected Nature of Web Client Applications, and Security Considerations, There ISN't a Perfect Way To make every web-based Crystal Report Automatically Print. But, Under Certain Conditions, We can provide this functionality in user-friendly ways.

Using Server-Side ReportDocument.PrintToPrinter Method This server-based method is documented in the Visual Studio help files Open the Help Index, and enter PrintToPrinter in the "Look for:".. Box The syntax for this method is: Report.PrintToPrinter ( , , , ) 'Collated' in this context has nothing to do with database collation. Some advanced printers (like copier / printers) will sort each document into its own shelf. Not every printer supports this functionality, so check the printer dialog before setting to true. to print the entire report, set startpage and endpage each to 0. An example in use might look like MyReport.PrintToPrinter ( 1, False, 0,0) One limitation of this method is that a printer name must be specified. you can set the default printer at design time in the report, and you can change the printer name at run time by setting the ReportDocument. PrintOptions.printername Property (The PrintOptions are also where you can assign page margins, portrait / landscape, etc.). Keep in mind that this method prints from the server itself, not from the client machine. This means that any printer you wish to use must be accessible from the server You cannot Print to a client '

s desktop printer using this method unless that printer is shared on the network and mapped to the server. If your LAN has networked printers, you can make some guesses as to which printer to assign as the default in each report. For instance, if the accounting Department has a departmental printer, that would be a good choice to use for the default in an accounting report. You can provide users the option to choose a networked printer by enumerating the printers mapped to the server and populating a drop-down list. The PrinterSettings.InstalledPrinters property returns a collection of installed printers, and can be bound to a DropDownList as below. DropDownList1.DataSource = System.Drawing.Printing.PrinterSettings.InstalledPrinters

DropDownList1.DataBind () Again, since this code is executing on the server, this will only enumerate the printers on the server mapped to the System user. If a printer does not appear in the drop-down list, you need to ensure that it is properly mapped to the System user (see below). All printers and the .NET Framework need to be mapped to the System user in order for this to work. This is not a trivial task to configure. Printers can be mapped to the system account only by editing the registry, and since the Framework is mapped to the System user, you may need to reassign security permissions on files and folders. This process is outlined in the "Printing Web-Based Reports From the Server" article in the VS .NET HELP FILES (Look Up "Crystal Reports, Printing" in the vs help index to link to the articles. The process is a little too intricate to cover Here Leave Comments if Help is needed, and i can amend the article later.

Client Side JavaScript: window.Print A second option you have uses the JavaScript window.Print method Adding this method to the onLoad event of the BODY tag will trigger the browser to print the current page Adding this method to a button.onClick event.. can also be done, just make sure you are using a standard HTML button and not an ASP.NET button control (if you do use an ASP.NET button control, you'll cause a postback, not execute the JavaScript). In a future article, we'll look at adding a print button to a custom CrystalReportViewer toolbar that uses this method to print a report. The window.print method does cause a printer selection window to open. The user will have a chance to select the printer they want to use and must click the "Print" button to start printing. There is no way to disable the prompt with JavaScript, so for Netscape and other browsers, users will have to see the dialog. However, in some versions of Internet Explorer , a call to the execwb ole method from a second browser object can be used to circumvent the print dialog (note: spaces have been added to OBJECT declarations to display the code properly in this article - you should remove the spaces to use the code): function PrintWindow () {if ( Navigator.Appname == "Microsoft Internet Explorer") {

Var PrintCommand = '

PrintCommand = 'ClassID = "CLSID: 8856F961-340A-11D0-A96B-00c04FD705A2">';

Document.body.insertadjacenthtml ('Beforend', PrintCommand);

PrintCommandObject.execwb (6, -1); printcommandObject.outerHTML = "";

else {window.print ();}..} The ExecWB method requires IE 5.5 or higher to function This script essentially creates another browser object of size 0 at the end of the current page This new browser object then issues a print command without prompting .. the user A limitation of the window.Print method is that only the current window will print In order to print all pages, you must set the SeparatePages property of your CrystalReportViewer to False when the report is rendered For example:. CrystalReportViewer1.SeparatePages = FALSE

CrystalReportViewer1.ReportSource = MyReport Setting SeparatePages to False will cause the entire report to be shown in the current window. The user will not have to page through the report, but a lot of scrolling may be required. Page breaks will appear wherever the browser puts them - there is no way to control where page breaks occur -. and the browser may wrap the layout in order for it to fit the printer page dimensions your formatting may be completely lost using the window.Print method if your report is wider than the browser window. The basic window.Print method also prints everything in the window, including whatever form inputs you have added, and the CrystalReportViewer toolbar. If you have set SeparatePages = False, you probably do not need the toolbar displayed. If you do not have any other inputs or buttons on your report page, turning off the toolbar may be all you need to do. If you did add inputs or other buttons, having them show on the printout will not look very pro fessional. To make the report appear a little more professional, we can enclose the report output in a DIV (by enclosing the CrystalReportViewer in a DIV or Panel) and then print just that DIV using a JavaScript function that opens a new window and includes only the information inside the DIV In a future article, we'll look at "skinning" the toolbar so you can have a toolbar and this printing functionality at the same time The basic code is shown below (source: Reference 2; note that.. spaces have been added in some tag names for them to display in this article - you should remove the spaces to use the code): var gAutoPrint = true; // Flag for whether or not to automatically call the print function

Function printspecial () {if (Document.getElementByid! = null) {

Var html = '/ n / n';

IF (Document.GetElementsBytagname! = null) {

Var headtags = document.getElementsBytagname ("head");

IF (Headtags.Length> 0)

HTML = Headtags [0] .innerhtml;

}

HTML = '/ n / n / n';

VAR PrintReadyelem = Document.GtelementByid ("PrintReady");

IF (PrintReadyelem! = null) {

HTML = PrintyElem.innerhtml;}

Else {

Alert ("Could Not Find The PrintReady Section In the HTML"); Return;}

HTML = '/ N

/ N

"

Var Printwin = Window.Open ("", "PrintSpecial");

Printwin.document.open ();

Printwin.document.write (HTML);

Printwin.document.close ();

IF (Gautoprint) printwin.print ();

}

Else {

Alert ("Sorry, The Print Ready Feature IS Only Available In Modern Browsers.");

}} If you want to get really fancy, you can combine the two methods so that a report window will automatically open and print in IE browsers or open and prompt for printing in non-IE browsers. The combined code is shown below and has been tested in both IE 6.0 and FireFox 0.9 (note: spaces have been inserted into tag names so code will display properly - you should remove the spaces to use the code): var gAutoPrint = true; // Flag for whether or not to automatically Call the print function

Function printspecial () {

IF (Document.GetElementByid! = null) {

VAR HTML = ' / n / n';

IF (Document.GetElementsBytagname! = null) {

Var headtags = document.getElementsBytagname ("head");

IF (Headtags.Length> 0) HTML = Headtags [0] .innerhtml;

}

IF (gautoprint) {

IF (Navigator.Appname == "Microsoft Internet Explorer") {

HTML = '/ N

/ n <'

HTML = 'B o D y online = "PrintCommandObject.execwb (6, -1);"> / n';

}

Else {

HTML = '/ N

/ n / n ';

}

Else {

HTML = '/ N

/ n / n ';

}

VAR PrintReadyelem = Document.GtelementByid ("PrintReady");

IF (PrintReadyelem! = null) {

HTML = PrintyElem.innerhtml;

}

Else {

Alert ("Could Not Find The Printready Section In the HTML");

Return;

}

IF (gautoprint) {

IF (Navigator.Appname == "Microsoft Internet Explorer") {

HTML = '

HTML = 'ClassID = "CLSID: 8856F961-340A-11D0-A96B-00c04FD705A2">

/ N

/ N

';}

Else {

HTML = '/ N

/ N

"

}

Else {

HTML = '/ N

/ N

"

}

Var Printwin = Window.Open ("", "PrintSpecial");

Printwin.document.open ();

Printwin.document.write (HTML);

Printwin.document.close ();

IF (gautoprint) {

IF (Navigator.Appname! = "Microsoft Internet Explorer") {

Printwin.print ();

}}}

Else {

Alert ("Sorry, The Print Ready Feature IS Only Available In Modern Browsers.");

}

Export to PDF As you may have learned from experience, the output from a CrystalReportViewer does not always look like you had intended. That is because the output is rendered as HTML and sent to the browser that then generates the web page. In order to get the exact formatting you want, you should export your reports as PDF documents. One advantage of doing so is that most users know how to print from the Acrobat Reader. Also, users can save reports and send them as e-mail attachments. One downside to this option is that users must start the printing process themselves, which does not solve the problem of minimizing user interaction. Unfortunately, since a PDF is not a page rendered by the browser, none of the JavaScript tricks above will work for us. There IS No Simple Way to Automatically Print An Exported Crystal Report In Pdf Format.

转载请注明原文地址:https://www.9cbs.com/read-103923.html

New Post(0)