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 = '< O B J E C T ID="PrintCommandObject" WIDTH=0 HEIGHT=0 ';
PrintCommand += 'CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">';
document.body.insertAdjacentHTML('beforeEnd', 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 don't 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 professional. 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< / H E A D >\n< B O D Y>\n';
var printReadyElem = document.getElementById("printReady");
if (printReadyElem != null) {
html += printReadyElem.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 = '< H T M L >\n< H E A D >\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 onLoad="PrintCommandObject.ExecWB(6, -1);">\n';
}
else {
html += '\n\n< B O D Y >\n';
} }
else {
html += '\n\n< B O D Y >\n';
}
var printReadyElem = document.getElementById("printReady");
if (printReadyElem != null) {
html += printReadyElem.innerHTML;
}
else {
alert("Could not find the printReady section in the HTML");
return;
}
if (gAutoPrint) {
if (navigator.appName == "Microsoft Internet Explorer") {
html += '< O B J E C T ID="PrintCommandObject" WIDTH=0 HEIGHT=0 '
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.");
}}
|
请发表评论