|
In some situations, a developer may want to automatically print a report when a user loads a page in a Web browser. A useful technique to do this, while still maintaining a report's page settings, is to use the WebViewer with the ActiveXViewer ViewerType. However, this technique works best in a controlled environment, such as a corporate intranet, where a client system's hardware, software, security settings, etc. are already known. This is not recommended when a client's environment is unknown, such as for a general audience on the Internet.
It is important to note that ViewerTypes that rely on HTML will not maintain a report's page settings, such as PageWidth and PageHeight. This is a limitation of HTML content. In order to maintain page settings, use either ActiveXViewer or AcrobatReader for the ViewerType property. The following article details using the WebViewer control with the ActiveXViewer ViewerType. Note that the WebViewer control applies to the professional version of ActiveReports for .NET. For printing through the ActiveXViewer without using the WebViewer control, see the following article.
HOWTO: Printing on the Client Using ASP.NET
http://www.datadynamics.com/forums/38962/ShowPost.aspx
For an introduction on how to use the WebViewer, please see the walkthrough titled, "Walkthrough: Web Viewer" in the ActiveReports for .NET documentation.
http://www.datadynamics.com/Help/ARNET3/ar3wlkWebViewerControl.html
In addition, the HTTP Handlers must be configured in order to use the WebViewer. See the "Configuring the HTTP Handlers" walkthrough in the help documentation for more information.
http://www.datadynamics.com/Help/ARNET3/ar3wlkConfiguringtheHTTPHandlers.html
The first step is to create or open an ASP.NET Web Application inside Visual Studio, and then add a WebViewer control to a Web Form. Next, set the WebViewer's ViewerType property to ActiveXViewer. The ReportName property can specify which ActiveReport in your current project to display in the WebViewer. However, specifying an ActiveReport to display in code by using just the Report property can allow for extra configuration and processing, as this article will demonstrate.
After the Web Application is set up with a WebViewer control, the next step is to add a DIV or other element to the Web Form's HTML that supports adding code at runtime. The HtmlContainerControl.InnerHTML property is ideal for this purpose. To use a DIV tag as an HTML Server control in ASP.NET, first add opening and closing DIV tags to a Web Form's HTML and then add the runat="server" attribute. Be sure to specify an id attribute as well so that the control is accessible from code.
<div id="divPrintScript" runat="server"></div>
Next, write a script that will run client-side on a user's Web browser to print the report when the page loads. (Later, use this to build a string in the code behind and set it to the InnerHTML property of the DIV tag.) Either JavaScript or VBScript will work. However, VBScript will provide better compatibility with the ActiveX control as well as attaching to the LoadCompleted event, which is the most suitable location for report printing code. Furthermore, since the viewer control uses ActiveX technology, Microsoft Internet Explorer may be the most suitable target browser to consider while developing an application that uses the ActiveXViewer.
Regarding client-side coding, it is important to note that the name of the ActiveXViewer in the client-side code will be a 'm' character followed by the name of the WebViewer control on the server, followed by "_ActiveX." For example, if the name of a WebViewer control is "WebViewer1," then the name of the ActiveXViewer control will be "mWebViewer1_ActiveX."
The following HTML and VBScript code will print a report. Omit the line that sets the DeviceName property to use the default printer.
<script language="vbscript"> <!-- Sub mWebViewer1_ActiveX_LoadCompleted() Dim arv Set arv = document.all("mWebViewer1_ActiveX") 'get a reference to the ActiveXViewer arv.Object.Printer.DeviceName = "Name of printer on user's system" 'define which printer to use 'print all pages arv.Object.PrintReport(false) End Sub --> </script>
The following alternative code will start a print job to print only certain pages of a report.
<script language="vbscript"> <!-- Sub mWebViewer1_ActiveX_LoadCompleted() Dim arv Set arv = document.all("mWebViewer1_ActiveX") 'get a reference to the ActiveXViewer arv.Object.Printer.DeviceName = "Name of printer on user's system" 'define which printer to use 'print only a range of pages arv.Printer.StartJob "My Report" dim i for i = 0 to 2 'print pages 1 through 3 (0-based index) if i >= 0 then if i >= arv.Object.Pages.Count then exit for arv.Printer.PrintPage arv.Object.Pages(i) end if next arv.Printer.EndJob End Sub --> </script>
The next step is to write code that will instantiate a new report object on the server and link it to the WebViewer control. An important aspect to note about this part is that ActiveReports for .NET will attempt to read the settings from the default printer, or from the specified printer to obtain various page setting for a report. Furthermore, in this situation there are two possible printers to consider. Therefore, using virtual printer mode and explicitly assigning page settings will eliminate many printing factors to consider.
For the server, we can specify virtual printer mode by setting the PrinterName property to an empty string. The PageSettings property will provide access to the report's page settings. Note that a report is static once rendered, so these properties will need to be set before the report is run, as the following demonstrates.
Dim rpt As New ActiveReport1 rpt.Document.Printer.PrinterName = "" rpt.PageSettings.PaperKind = Printing.PaperKind.Custom rpt.PageSettings.PaperWidth = 8.5 '8.5 inches rpt.PageSettings.PaperHeight = 11 '11 inches
Next, use the client-side printing script code to build a string to assign to the HTML Server Control or Web Control. This will need to ensure that the viewer settings match those of the report. Be sure to account for line breaks such as carriage return/line feeds or semicolon ';' characters. In addition, escape certain characters as necessary for the server and client languages.
We can also set the PaperWidth and PaperHeight properties of the ActiveXViewer in this script. This is important to do since it will default to using the client system's default printer setting. Note that the report's default unit of measurement is inches, but the ActiveXViewer's default unit is twips. One inch is equal to 1440 twips.
The following ASP.NET/VB.NET code will build a string for Internet Explorer/VBScript.
'build auto print VBScript for client browser Dim strAutoPrintScript As String = "" strAutoPrintScript += "<script language=""vbscript"">" + vbNewLine strAutoPrintScript += "<!--" + vbNewLine strAutoPrintScript += "Sub mWebViewer1_ActiveX_LoadCompleted()" + vbNewLine strAutoPrintScript += "Dim arv" + vbNewLine strAutoPrintScript += "Set arv = document.all(""mWebViewer1_ActiveX"")" + vbNewLine strAutoPrintScript += "'uncomment to specify printer on client" + vbNewLine strAutoPrintScript += "'arv.Object.Printer.DeviceName = ""my printer's name""" + vbNewLine strAutoPrintScript += "arv.Printer.PaperWidth = 1440 * " + rpt.PageSettings.PaperWidth.ToString + vbNewLine strAutoPrintScript += "arv.Printer.PaperHeight = 1440 * " + rpt.PageSettings.PaperHeight.ToString + vbNewLine strAutoPrintScript += "'print all pages" + vbNewLine strAutoPrintScript += "arv.Object.PrintReport(false)" + vbNewLine strAutoPrintScript += "'print only a range of pages" + vbNewLine strAutoPrintScript += "'arv.Printer.StartJob ""My Report""" + vbNewLine strAutoPrintScript += "'Dim i" + vbNewLine strAutoPrintScript += "'for i = 0 to 1 'print first 2 pages" + vbNewLine strAutoPrintScript += "'if i >= 0 then" + vbNewLine strAutoPrintScript += "'if i >= arv.Object.Pages.Count then exit for" + vbNewLine strAutoPrintScript += "'arv.Printer.PrintPage arv.Object.Pages(i)" + vbNewLine strAutoPrintScript += "'end if" + vbNewLine strAutoPrintScript += "'next" + vbNewLine strAutoPrintScript += "'arv.Printer.EndJob" + vbNewLine strAutoPrintScript += "End Sub" + vbNewLine strAutoPrintScript += "-->" + vbNewLine strAutoPrintScript += "</script>" + vbNewLine 'add script string to div Me.divPrintScript.InnerHtml = strAutoPrintScript
Finally, in some situations, a developer may not want the user to see the report in the Web browser. To hide the ActiveXViewer while still maintaining report printing functionality, set the WebViewer's Width and Height properties to zero in the code behind.
Me.WebViewer1.Width = New System.Web.UI.WebControls.Unit(0) Me.WebViewer1.Height = New System.Web.UI.WebControls.Unit(0)
To see a working application that demonstrates the concepts in this article, download the attached ASP.NET/VB.NET sample project.
Applies To: ActiveReports for .NET 3.0
|