Also talk about web printing (3): let alone IE to implement our own printing template

Source: Internet
Author: User
In the previous articleArticle《 AlsoWebPrint (2): A Simple AnalysisIETo achieve simple printing and preview. We have analyzed IE And learned how to call IE In this article, we write our own printing template.

Slave Ie5.5Start, you can customize Webbrowser Controls and IE How to print and preview documents. The printing and preview mechanisms include the printing template control. Html Files, which can be generated by developers using the object model exposed by the print template behavior. Msdn If the print template is Html The custom print template can only be C ++ Language from publishing or Interception Idm_print/idm_printpreview CommandProgramOr control (print the template path from Iolecommandtarget: Exec Of Pvain Parameter ). But in fact, Delphi , C #The language is also acceptable. I have already tested it. As VB No, I have never tried it. I'm not sure. By default, IE Use your own print template, that is, if Iolecommandtarget: Exec Of Pvain The parameter is Null When, IE You can call your own printing template to print the template.

What can we do with a print template?
1,Control the page layout, printing and preview content during printing and previewing;
2,Controls how print tasks are processed, for example, in which order pages are printed;
3,Controls the appearance of the print preview window, and places custom controls on the print user interface.

With the print template, we can add some very cool functions, such as joining the company's Logo , Legal statement, advertisement, customization of the position and style of the header and footer, arrangement of printing plan for timed printing, and so on.
A printing template is actually a standard Html File, and other Html The difference between files is that 4 Basic elements of the Declaration template: Layoutrect , Devicerect , Templateprinter And Headerfooter . The following describes how 4 Template elements:
Layoutrect : Generate a container for the document content in the print and preview templates .
Devicerect : Layoutrect Elements and other content in the print template provide a container. Located in Devicerect Other content will not be printed.
Templateprinter : Provides a print template to get page settings and print settings, and can control the print job initialized from the print template.
Headerfooter : Provides a tool for printing templates to convert the formatted strings of the header and footer into formatted strings. Html Text.
Next, we will first write a minimum template with preview but not printing (TemplateCodeFrom Msdn):

1 <! -- Template1.htm: Minimum template (Preview only)
2 This template shows the smallest template. It supports print preview, but cannot be printed. It only contains two devicerect elements, each of which contains a layoutrect element. During the preview, the original document is not fully displayed. The layoutrect element's style sheet defines the required width and height attributes. Other attributes are not required. The layoutrect and devicerect elements contain color and border attributes (white and yellow respectively), making it easy to recognize their border. The size of the devicerect element is 8.5x11 inch, which is not the actual value in IE "page settings.
3 -->
4 < Html Xmlns: IE >
5 < Head >
6 <? Import namespace = "ie" implementation = "# default">
7 <Style type = "text/CSS">
8 . Lorstyle
9 {
10 Width: 5.5in;
11 Height: 8in;
12 Margin: 1In;
13 Background: white;
14 Border: 1 dashed gray;
15 }
16 . Pagestyle
17 {
18 Width: 8.5in;
19 Height: 11in;
20 Background: # ffff99;
21 Border-left: 1 solid black;
22 Border-top: 1 solid black;
23 Border-Right: 4 solid black;
24 Border-bottom: 4 solid black;
25 Margin: 10px;
26 }
27 </Style>
28 </Head>
29 <Body>
30 <Ie: devicerect id = "page1" class = "pagestyle" Media = "print">
31 <Ie: layoutrect id = "layoutrect1" contentsrc = "document" class = "lorstyle" nextrect = "layoutrect2"/>
32 </Ie: devicerect>
33 <Ie: devicerect id = "page2" class = "pagestyle" Media = "print">
34 <Ie: layoutrect id = "layoutrect2" class = "lorstyle"/>
35 </Ie: devicerect>
36 </Body>
37 </Html>
38

Save the above Code and run the code I used in the previous article, document address www.cnblogs.com , click preview . Its effect is as follows:

The above example is not complete yet. Print and preview will only show2Page, no matter how many pages you have, it will only display the front2Page. The page is also fixed, no matter what the actual page size is, orIEIn "page settings", the size is not adjusted.8x11.5The page size in inches is displayed. In addition, printing is not supported, even if you pressPrint"Button, it does not print, but only executes the preview action.

We noticed that each label starts with a namespace Declaration ( IE: ), A print template must declare its namespace in the following form in the first line:
< Html Xmlns:IE>
< Head >
<? Import namespace ="Ie"Implementation ="# Default">
Here, Html Label XmlnsThe template declares IE You can also IE "Replace it with any name you like. Of course, all the namespaces in the back must be changed to the name you like. Import The tag is then imported Internet Explorer Pair IE The implementation of the namespace makes the behavior of elements on the print template effective to the template.

It is worth noting that, Layoutrect There is no end label, and a backslash must be placed before the end bracket, which is similar XML The syntax is the same for elements without end tags in the document. Layoutrect Cannot contain any Html For example, the following statement is not valid:
Layoutrect1.innerhtml = "<B> Hi there! </B> ";
Now let's look at the attributes. ID Attributes and Class Attributes are well-known and worth noting, Class The specified style must contain Width And Height . In this example 2 Values do not match the actual conditions. Of course, we can Templateprinter Obtain the correct value in the action. But we will not discuss this first. Another attribute is Mediea Everyone knows what it means. However, here we must specify MediaThe attribute value is" Print ", This is mandatory. In addition Contentsrc If the value is" Document To print or preview IE current display If you do not want to print this document, you can specify other URL For example Http://yahong111.cnblogs.com, In this way, it will print or preview Http://yahong111.cnblogs.com Instead of the current page. The last one is Nextrect If the current page is full Layoutrect In this example, Layoutrect1 " NextrectIs" Layoutrect2 ", If the current page is the last page, do not specify Nextrect . The number of pages of printed or previewed documents must be Layoutrect . In this example, we only specify 2 Items Layoutrect So, only 2 Page, and even if the printed document has only one page, it will display 2 Page.
Below is Devicerect And Layoutrect .


How do we know the number of documents that are currently printed or previewed? Layoutrect What about it? How do we set each Devicerect And Layoutrect Of Width And Height What about it? This is what we will discuss in the next article.
Reference: Beyond print preview: Print customization for Internet Explorer 5.5"

In addition, add the enhanced Dlprinter. Cab Component. Now the component can set the direction of the header, footer, and paper (horizontal / Vertical ). Not Supported . Cab File Upload, so the dlprinter. Cab , Changed Dlprinter.cab.rar , Please change the file name to use it.
The usage is as follows:
<Object ID = "dlprinter" classid = "CLSID: 5c230622-45e5-4e3c-893c-3bfddc4db5e4" Height = "0" width = "0" codebase = "dlprinter. Cab"> </Object>
<SCRIPT>
Dlprinter. marginleft = 20;
Dlprinter. pageheader =" This is the header of the test. ";
Dlprinter. pagefooter =" This is the test footer. ";
Dlprinter. islandscape = 1 ;// Set the page to landscape
// Dlprinter. contenturl = http://www.cnblogs.com/yahong111/archive/2007/09/19/898326.html;
</SCRIPT>
<Input type = "button" id = "btnprint" value = "print preview" onclick = "dlprinter. printpreview ()"/>

welcome!

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.