WEB printing feature provided by IE-suitable for general printing
First, add the following content to the webpage:
<OBJECT id = "WebBrowser" height = "0" width = "0" classid = "CLSID: 8856F961-340A-11D0-A96B-00C04FD705A2"
VIEWASTEXT>
</OBJECT>
Then you can add the function buttons in sequence:
<Input onclick = "document. all. webBrowser. execWB (6, 1) "type =" button "value =" print "> <input onclick =" document. all. webBrowser. execWB (6, 6) "type =" button "value =" print ">
<Input onclick = "document. all. WebBrowser. ExecWB (8, 1)" type = "button" value = "page settings">
<Input onclick = "document. all. webBrowser. execWB (7,1) "type =" button "value =" print preview "> & nbsp; <INPUT type =" button "value =" Close window "onclick =" javascript: window. close () ">
Put the two items in <center class = noprint> </center> and the buttons will not be printed. Of course we need to define noprint:
<Style media = "print">. Noprint {DISPLAY: none} </style> you only need to set the css Of the things you don't want to print to noprint.
Now we have implemented basic web printing. Note the following:
You must set the Security Settings of internet Options of ie to prompt or enable ActiveX controls that are not marked as secure. Otherwise, an error will be reported, resulting in unavailability.
If you edit the page in the vs.net editing environment, it automatically adds additional parameters to the object. With these parameters, printing will fail, so remember to delete them when saving them.
To make it easy and achieve the best results, we can edit and print a page. At this time, a lot of textbox is required. We set its css to. edittext.
{
Overflow-y: visible;
Width: 100%;
Border-top: none;
Border-right: none;
Border-bottom: none;
Border-left: none;
.
There is also some small experience that when setting the width of the Standard grid, for A4 paper, horizontal use 920, vertical use 640, the best effect.
Web Printing
Execwb Parameters
Currently, when developing B/S structured programs, Web printing is one of the most difficult and troublesome problems. The printed results are often far from the expected ones. How can we print the desired results? The methods described in this article will solve this problem, making Web printing easy to use.
We know there are three ways to implement Web printing: the first is to use Office tools that everyone is familiar with to implement Web printing, such as using Word or Excel printing tools to implement Web printing, the difficulty of this method is how to import data from a Web page into Word or Excel. The second is to use the print control provided by IE browser to implement Web printing, the difficulty of this method is how to control the settings of browser printing controls in the program. The third is to use third-party controls or report software to implement Web printing, the difficulty of this method is that most of the report software is paid for use, so it has to be considered economically. For the third method, such controls include the FileSystemObject component and ScriptX. such as the cab control. This type of report software includes such software as Ruyi report, Cell plug-in of yonyou Company, and full-dynamic Web reports of beautiful girl. Here we will not describe their usage, as long as you refer to the instructions provided by them to control them. I will focus on the first two methods below.
Use Office tools for Printing
The first method is to use the Office printing tool for Web printing. We mentioned above that the difficulty of this method is to import data into Word or Excel. Next I will take Excel as an example to introduce how to import data into Excel. In fact, there are many ways to import data from webpages into Excel. Here we will only introduce one of them, that is, using ActiveX Control -- Excel. Application. Excel. Application is a programming interface provided by Microsoft for Excel. In other programming languages, you can use this interface program to Operate Excel tables. The following describes the commonly used commands used to operate EXCEL tables in a program (using the script language VBScript as an example, similar to other languages ):
1. Set xlApp = CreateObject ("Excel. Application") 'to create an EXCEL Object
2. Set xlBook = xlApp. Workbooks. Open ("file name") 'Open an existing Excel worksheet
3. Set xlBook = xlApp. Workbooks. Add () 'create an Excel worksheet
4. xlApp. Visible = true' sets the EXCEL object to be Visible (or invisible)
5. Set xlSheet = xlBook. Worksheets ("table name") 'sets the activity Worksheet
6. Set xlSheet = xlBook. ActiveSheet 'to Set the active Worksheet (Default table name)
7. xlSheet. Cells (row, col) = value 'assigns a value to the cell (row, col ).
8. Print the worksheet in xlSheet. PrintOut.
9. xlBook. Close (True) 'Close the workbook
10. xlApp. Quit: end the EXCEL Object
11. Set xlApp = Nothing 'to release the xlApp object
Here is a simple example to illustrate the usage of these commands. The following shows the key implementation code (implemented using javascript scripts and VBScript, which can be implemented by interested readers ):
<Script language = "javascript">
Function xlPrint (){
Var xlApp; // store Excel objects
Var xlBook; // store the Excel worksheet File
Var xlSheet; // store the Excel worksheet
Try {
XlApp = new ActiveXObject ("Excel. Application"); // create an Excel Object}
Catch (e ){
Alert ("enable ActiveX control settings! ");
Return ;}
XlBook = xlApp. Workbooks. Add (); // create an Excel Workbook File
XlSheet = xlBook. ActiveSheet; // activate an Excel worksheet
Var rowLen = printData. rows. length; // number of rows of the table object
For (var I = 0; I <rowLen; I ++ ){
Var colLen = printData. rows (I). cells. length; // Number of columns of the table object
For (var j = 0; j <colLen; j ++) // assign values to cells in an Excel table
XlSheet. Cells (I + 1, j + 1). value = printData. rows (I). cells (j). innerText ;}
XlApp. Visible = true; // set the Excel object to be Visible}
</Script>
Code Description: as long as the table ID in the program is set to: printData, and then the onclick Event Response Function of the print button is set to xlPrint; however, to run this program, IE must allow initialization and script running of ActiveX controls that are not marked as secure. The specific setting method is as follows: Open Control Panel → Internet Options → Security → Custom Level → initialize and Run ActiveX controls not marked as secure → select and enable, in this way, our program can run. If this ActiveX Control setting is not enabled, an exception is thrown when the program executes the creation of an Excel object. In this case, you can catch this exception through the catch () Statement and handle it accordingly. If you want to print it directly, you can call the command: xlSheet. PrintOut.
The preceding method imports data from cells on the Web page to cells in Excel. The following describes how to import the entire table into Excel. The main idea of this method is to copy the content to be printed on the webpage to the clipboard, and then call the xlSheet. Paste () command to copy the content of the clipboard to Excel. Let's take a look at its key code:
Var selectArea = document. body. createTextRange (); // create a text area
SelectArea. moveToElementText (printData); // focus the text area on printData
SelectArea. select (); // select printData
SelectArea.exe cCommand ("Copy"); // Copy printData to the clipboard
XlSheet. Paste (); // copy the data in the clipboard to Excel
Using IE browser
In the previous section, we used Excel as an example to describe how to use the Office printing tool to print Web pages. Now let's take a look at how to use the print control provided by IE browser to print Web pages. As we all know, Internet Explorer has powerful printing functions. Why don't we use this to implement Web printing. The reason is that if we directly use the print function of IE or call window in the program. print () to print the webpage, the webpage title and page number information will appear in the header, and the webpage and date information will appear in the footer. In fact, this information is not what we want to print, so how can we remove this information? The practice is actually very simple. We only need to open the page Setting Dialog Box in the File menu of IE, and then remove the header and footer code (header: & w & B page number, & p/& P footer: & u & B & d). If we print it again, the information will not appear. Now let's take a look at what these codes mean? In fact, these codes are provided by IE to set print pages. The specific meanings of these codes are given below, as shown in the table.
& W webpage title
& U web site (url)
& D short Date Format
& D long Date Format
& T current time format
& T 24-hour time format
& P current page number
& P total page number
Through the above table, the reader should understand the meaning of the default header and footer in the IE printing settings, and the reader can combine the above Code to set his/her favorite printing settings. Here we are not just talking about this. we imagine that we have developed a system based on the B/S structure, which contains the report section, we cannot allow each client to set the print settings of IE, so we need to control these settings in the program. Below we use VBScript to write a function to control page settings by modifying the registry key value. The Code is as follows:
<Script language = "VBScript">
Dim path, reg
'Path stores the Registry address set for IE printing, and reg stores the objects of the WScript. Shell component.
Path = "HKEY_CURRENT_USER \ Software \ Micro-soft \ Internet Explorer \ PageSetup"
'Modify the print settings through the registry, and only modify the values of the header, footer, and each boundary.
'Parameter Description: header -- header, footer -- footer, margin_left -- left boundary
'Margin _ top -- upper boundary, margin_right -- right boundary, margin_bottom -- lower boundary
'In the margin settings, 1 corresponds to 25.4mm, that is, margin_left = 1 indicates 25.4mm of the actual value.
Function pagesetup (header, footer, margin_left, margin_top, margin_right, margin_bottom)
On Error Resume Next
Set reg = CreateObject ("WScript. Shell ")
If err. Number> 0 then
MsgBox "cannot create WScript. Shell object! "
Exit function
End if
Reg. RegWrite path + "\ header", header' sets the header
Reg. RegWrite path + "\ footer", footer 'sets the footer
Reg. RegWrite path + "\ margin_left", margin_left 'sets the left boundary.
Reg. RegWrite path + "\ margin_top", margin_top 'sets the upper boundary.
Reg. RegWrite path + "\ margin_right", margin_right 'sets the right border
Reg. RegWrite path + "\ margin_bottom", margin_bottom 'sets the bottom boundary
End function
</Script>
In the program, you can write another function (which can be written by VBscript or javascript script) to call the function first, and then call window. print () to print the function. No code is provided here. Careful readers will surely find that the printed page will have a print button on it, and the page is divided by the IE printing control according to the paper size, next we will introduce a method to control the display of pages and print buttons. This method uses CSS style sheets to control these settings, because the style sheet has a "media = print" attribute, which takes effect only when being printed, therefore, you can control the printing buttons and pages as follows:
<Style media = print>
. Noprint {display: none ;}
. Pagenext {page-break-after: always ;}
</Style>
You only need to add the code class = "noprint" to the print button, and add the Code <div class = "pagenext"> & lt;/div> to the page to be paged. So far, our printing function has been quite complete, but there is no print preview and direct printing function. Generally, users will preview it before printing. If there is no problem, print it directly. The following two functions are implemented through the IEWebBrowser component.
First, create the IEWebBrowser object:
<Object id = WebBrowser classid = CLSID: 8856F961-340A-11D0-A96B-00C04FD705A2 width = 0 height = 0> </object>
The ExecWB method is called with different parameters to implement print preview and direct printing:
WebBrowser. ExecWB (6, 1); // print
WebBrowser. ExecWB (6, 6); // print directly
WebBrowser. ExecWB (); // print preview
There are many other parameters in the ExecWB method. I will not describe them here.
How do I remove the path at the bottom of the page and the page number at the top of the page when printing a page?
Ie File-> page settings-> the content in the header and footer is removed, and the print will not be available.
(3) print and remove/Add the header and footer
Reference:
<Script language = "JavaScript">
Var hkey_root, hkey_path, hkey_key
Hkey_root = "HKEY_CURRENT_USER"
Hkey_path = "http://www.cnblogs.com/chenbg2001/admin/file://Software//Microsoft//Internet Explorer \ PageSetup \\"
// Set the page header and footer to be empty.
Function pagesetup_null (){
Try {
Var RegWsh = new ActiveXObject ("WScript. Shell ")
Hkey_key = "header"
RegWsh. RegWrite (hkey_root + hkey_path + hkey_key ,"")
Hkey_key = "footer"
RegWsh. RegWrite (hkey_root + hkey_path + hkey_key ,"")
} Catch (e ){}
}
// Set the page header and footer to the default value.
Function pagesetup_default (){
Try {
Var RegWsh = new ActiveXObject ("WScript. Shell ")
Hkey_key = "header"
RegWsh. RegWrite (hkey_root + hkey_path + hkey_key, "& w & B page number, & p/& P") hkey_key = "footer"
RegWsh. RegWrite (hkey_root + hkey_path + hkey_key, "& u & B & d ")
} Catch (e ){}
}
</Script>
<Input type = "button" value = "Clear page number" onclick = pagesetup_null ()>
<Input type = "button" value = "Restore page number" onclick = pagesetup_default ()>
Copy the file and check the effect.
Paging
<Style media = "print">
. PageNext {page-break-after: always ;}
</Style>
<Div class = "PageNext"> </div>
Add this sentence to the page.