Web page Printing--Print specified area, page preview, page Setup

Source: Internet
Author: User
Tags print print

Reprint http://hi.baidu.com/zyb512/blog/item/e9cf70441ce0984a500ffe79.html
1. Use the component to generate a Word document or an Excel document from the contents of the report, and export it to print using Office's own printing capabilities

2. Use JavaScript or other language-based builds on your Web page

These two methods are described in detail below

1. Generate Word or Excel document, when the content of the area to be printed as text is suitable for export to Word document processing, when the content to be printed as a table suitable for export as an Excel document, in the JSP can use Jxl,jacob, POI and other building for Office document processing, personal referral to use POI, this component is developed and maintained by the Apache Fund, its position in the open source field is undoubtedly. The use of this component is no longer described in detail, and its help document has detailed usage and sample code. Can meet a variety of basic development needs.

2. There are more ways to use JavaScript or print components, and here's a quick introduction to several scenarios and detailed code for one of these scenarios.

A. Use plain JavaScript code:

This method is simply called the window.open () method, the effect of the method is equivalent to the use of IE printing function, the entire page will see the content of all the printing, and will automatically print out the header footer.

B. Modifying registration entries, and other solutions for a particular browser

The principle of this method is basically to enter the registry of the client system, some of the Internet Explorer options to modify, such as masking or modify the header and footer, such as custom print-side scheme, the drawbacks of this aversion is very obvious, first to modify the registry key on the Web page requires the client to reduce the browser security settings, This poses a significant risk to the client's computer, and these methods are to locate the browser in the registry and then operate, limiting processing to one or several browsers, and also restricting the client's operating system to Windows

C. Third-party plugins

Using third-party plug-ins, such as Scriptx,webprint, and so on, this third-party principle is basically the same as the B scheme, there are some pure JavaScript code for encapsulation, but the use of this plug-in requires the client to install the plug-in when printing, also need to reduce the client's security settings , if there is a firewall or the impact of anti-virus software will not play a role, and the speed of this program is very problematic, loading plug-in processing seriously affect the speed of operation, but the printing effect of this scheme is very ideal

D. Compromise programme

Combined with WebBrowser and JavaScript and CSS.

Print the specified area
The Print Print Preview Page Setup button is displayed on the page to set up the print so you can remove the header and footer
Use CSS controls in JavaScript to hide what you don't want to print
Here is the detailed code (note: The construction code is linked together, otherwise JavaScript interprets the error) because of the use of JavaScript to construct a new page:

   <HTML>     <Head>    <Scriptlanguage= "JavaScript">      functionPrintme () {varPrint=  This. document.getElementById ("Print"). InnerHTML; Print=Print+"<br/>"Print=Print+'<script language=javascript> function Printview () {hidden ();d Ocument.all.WebBrowser.ExecWB (7,1);} function print () {hidden ();d Ocument.all.WebBrowser.ExecWB (6,6)}function PageSetup () {hidden (); Document.all.WebBrowser.ExecWB (8,1);} function hidden () {document.all ("Printview"). style.display= "None"; document.all ("print"). style.display= "None"; document.all ("PageSetup"). style.display= "None";} <\/script>'; Print=Print+ "<object classid=clsid:8856f961-340a-11d0-a96b-00c04fd705a2 height=21 id=webbrowser width=87></OBJECT& Gt   <input Id=printview Name=button onclick=printview () Type=button value= print preview > <input id = print Name=Button   Onclick=print () Type=button value= Direct print > <input id = pageSetup Name=button onclick= pageSetup () Type=button Value= Page setup >"; varNewWindow=window.open (); NewWindow.document.open ("text/html");           NewWindow.document.write (print);        NewWindow.document.close (); } </Script> </Head> <Body>     <spanID= ' print '>Put the content you want to print here</span> <P>All content</P> <ahref= "Javascript:printme ()"Target= "_self">Print</a>    </Body> </HTML> 



Code explanation: Put the content to be printed in the area with the ID of print in the Printme function to the output of the content into a new page, in the new page to print processing, in the new page with the WebBrowser object for printing processing, with CSS control to hide what you do not want to print.

Web page Printing--Print specified area, page preview, page Setup

Web page Printing--Print specified area, page preview, page Setup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.