Automatically Printing Crystal Reports in ASP.NET
发表于2004/10/20 7:31:00 2155人阅读
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.
|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:
'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
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.
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.) and also in Reference 3 (at the end of this article). The process is a little too intricate to cover here (leave comments if help is needed, and I can amend the article later).
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:
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 don't need the toolbar displayed.
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):
|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.
|Conclusion and References|
In a controlled networked environment, the PrintToPrinter() method may provide all the functionality you need. Networked printers can be enumerated in a drop-down list, and the the user can choose which printer to use. Most of the limitations have to do with the Internet being a disconnected medium and browser security, which also limits intranet functionality. Using the PrintToPrinter method requires editing the registry and remapping the .NET Framework user context and limits printing to networked printers.
Printing Crystal Reports is not a process that can be easily automated in a very satisfactory manner. There is no single magic solution to fit every need; you will need to see which option works best in each situation. Future versions of Crystal Reports .NET may contain additional functionality, and there are additional tricks we can use in Crystal Reports 10 that will be looked at in a future article.
3) "Printing Web based reports with Crystal Reports for Visual Studio .NET", http://support.businessobjects.com/communityCS/TechnicalPapers/crnet_web_app_printing.pdf.asp
Developing Crystal Reports applications in PowerBuilder .NET
lively1982 2014-10-24 16:25 696
Crystal Reports for Visual Studio .NET SDK API Reference Guide
glaclal 2013-02-23 10:50 8
Running ASP.NET 5 applications in Linux Containers with Docker
afandaafandaafanda 2015-10-09 10:42 682
kittyjie 2008-05-11 11:14 84
Crystal Reports for VS.NET
u9011 2008-02-11 12:17 1324
英文版的 Professional Crystal Reports® for Visual Studio®.NET
jinbiao_23 2012-03-05 08:12 4
SAP Crystal Reports .NET API Guide
niubegin 2012-01-29 23:53 5
How to convert XML to JSON in ASP.NET C#
Joyhen 2014-07-09 01:04 1112