【问题标题】:HTML generated Microsoft Word document with header, footer and watermarkHTML 生成的带有页眉、页脚和水印的 Microsoft Word 文档
【发布时间】:2012-10-31 16:05:09
【问题描述】:

我正在寻找由 HTML 生成的 Microsoft Word 文档的示例。 它应该包含页眉、页脚和水印。

【问题讨论】:

    标签: html ms-word watermark


    【解决方案1】:

    这是一些例子,希望它可以帮助其他试图用 HTML 生成 ms word .doc 文件的人。

    <html xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:w="urn:schemas-microsoft-com:office:word"
    xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
    xmlns="http://www.w3.org/TR/REC-html40">
    <head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><title></title>
    <style>
    v\:* {behavior:url(#default#VML);}
    o\:* {behavior:url(#default#VML);}
    w\:* {behavior:url(#default#VML);}
    .shape {behavior:url(#default#VML);}
    </style>
    <style>
    @page
    {
        mso-page-orientation: landscape;
        size:29.7cm 21cm;    margin:1cm 1cm 1cm 1cm;
    }
    @page Section1 {
        mso-header-margin:.5in;
        mso-footer-margin:.5in;
        mso-header: h1;
        mso-footer: f1;
        }
    div.Section1 { page:Section1; }
    table#hrdftrtbl
    {
        margin:0in 0in 0in 900in;
        width:1px;
        height:1px;
        overflow:hidden;
    }
    p.MsoFooter, li.MsoFooter, div.MsoFooter
    {
        margin:0in;
        margin-bottom:.0001pt;
        mso-pagination:widow-orphan;
        tab-stops:center 3.0in right 6.0in;
        font-size:12.0pt;
    }
    </style>
    <xml>
    <w:WordDocument>
    <w:View>Print</w:View>
    <w:Zoom>100</w:Zoom>
    <w:DoNotOptimizeForBrowser/>
    </w:WordDocument>
    </xml>
    </head>
    
    <body>
    <div class="Section1">
    
        <p>&nbsp;</p>
    <br/>
        <table id='hrdftrtbl' border='0' cellspacing='0' cellpadding='0'>
        <tr><td>        <div style='mso-element:header' id=h1 >
            <!-- HEADER-tags -->
                <p class=MsoHeader >HEADER</p>
            <!-- end HEADER-tags -->
            </div>
        </td>
        <td>
        <div style='mso-element:footer' id=f1><span style='position:relative;z-index:-1'> 
            <!-- FOOTER-tags -->
            FOOTER
    
            <span style='mso-no-proof:yes'><!--[if gte vml 1]><v:shapetype
             id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t"
             path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
             <v:formulas>
              <v:f eqn="if lineDrawn pixelLineWidth 0"/>
              <v:f eqn="sum @0 1 0"/>
              <v:f eqn="sum 0 0 @1"/>
              <v:f eqn="prod @2 1 2"/>
              <v:f eqn="prod @3 21600 pixelWidth"/>
              <v:f eqn="prod @3 21600 pixelHeight"/>
              <v:f eqn="sum @0 0 1"/>
              <v:f eqn="prod @6 1 2"/>
              <v:f eqn="prod @7 21600 pixelWidth"/>
              <v:f eqn="sum @8 21600 0"/>
              <v:f eqn="prod @7 21600 pixelHeight"/>
              <v:f eqn="sum @10 21600 0"/>
             </v:formulas>
             <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
             <o:lock v:ext="edit" aspectratio="t"/>
            </v:shapetype><v:shape id="Picture_x0020_1" o:spid="_x0000_s3073" type="#_x0000_t75"
             alt="VHB" style='position:absolute;
             margin-right:0pt;margin-top:-400pt;
             z-index:-1;
             visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt;
             mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;
             mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;
             mso-position-horizontal-relative:text;mso-position-vertical:absolute;
             mso-position-vertical-relative:text'>
             <v:imagedata src="https://www.google.bg/logos/2012/Rodin-2012-homepage.png"/>
            </v:shape><![endif]--></span>
               <p class=MsoFooter>
               <span style='mso-tab-count:2'></span>
               Page <span style='mso-field-code: PAGE'><span style='mso-no-proof:yes'></span> from <span style='mso-field-code: NUMPAGES'></span>
    
            <!-- end FOOTER-tags -->
       </span>
    
    
            </p>
        </div>
    
    
    
        <div style='mso-element:header' id='fh1'>
            <p class='MsoHeader'><span lang='EN-US' style='mso-ansi-language:EN-US'>&nbsp;<o:p></o:p></span></p>
            </div>
            <div style='mso-element:footer' id='ff1'>
            <p class='MsoFooter'><span lang='EN-US' style='mso-ansi-language:EN-US'>&nbsp;<o:p></o:p></span></p>
        </div>
    
        </td></tr>
        </table>
    </div>
    
    </body></html>
    

    您可以通过在元素 v:shape id="Picture_x0020_1" 上设置宽度、高度、margin-top、margin-right 来调整水印图片:内联样式。

    【讨论】:

    • 嗨 Vencedor,你能写信告诉我如何在上面的代码中插入 html 文本而不是图像?
    • &lt;span style=mso-tab-count:2'&gt;&lt;/span&gt; 是否缺少单引号?不应该是&lt;span style='mso-tab-count:2'&gt;&lt;/span&gt;
    • 在域代码之后和单引号结束之前有多余的空格(例如PAGE '),这会导致公式行为异常
    • 嗨,这里的代码是什么来获取行号以及页眉和页脚?
    【解决方案2】:

    试试:

    <img src="http://www.website.com/path/to/image.jpg" width="width_in_points" height="height_in_points" style="width:width_in_poits;height:height_in_points;" />
    

    完整示例:

    <img src="http://www.website.com/path/to/image.jpg" width="233" height="54" style="width:233pt;height:54pt;" />
    

    我对 VML 和在 Word 中嵌入图像并不是非常熟悉,但就目前情况而言,它适合我的目的,并且我能够将其嵌入到 Word 文档中。 Word 将在首次加载时自动处理嵌入。

    请记住,Word 中的默认度量是厘米和磅,因此您必须全部以磅为单位。可能一个好方法是先嵌入图像,在 Word 中打开,如果需要,调整大小并通过 word 获取图像测量值。然后返回您的应用程序并设置宽度和高度。尽量保持与 Word 设置的比例相同,否则它会调整图像大小。至少这就是 Word 对我所做的。

    编辑:

    我修改了上面的图像示例 - 它适用于某些版本的 word,但不嵌入图像。每次打开文件时都会收到一条警报,询问您是否要下载外部文件。如果您的办公室安全设置阻止您这样做,您需要使用第一个答案嵌入文件,我已确认可与 office 2007+ 一起使用:

            <div style='mso-element:header' id=h1 >
                <span style='mso-no-proof:yes'>
                    <!--[if gte vml 1]>
                    <v:shapetype id="_x0000_t75"
                        coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe"
                        filled="f" stroked="f">
                        <v:stroke joinstyle="miter"/>
                        <v:formulas>
                            <v:f eqn="if lineDrawn pixelLineWidth 0"/>
                            <v:f eqn="sum @0 1 0"/>
                            <v:f eqn="sum 0 0 @1"/>
                            <v:f eqn="prod @2 1 2"/>
                            <v:f eqn="prod @3 21600 pixelWidth"/>
                            <v:f eqn="prod @3 21600 pixelHeight"/>
                            <v:f eqn="sum @0 0 1"/>
                            <v:f eqn="prod @6 1 2"/>
                            <v:f eqn="prod @7 21600 pixelWidth"/>
                            <v:f eqn="sum @8 21600 0"/>
                            <v:f eqn="prod @7 21600 pixelHeight"/>
                            <v:f eqn="sum @10 21600 0"/>
                        </v:formulas>
                        <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
                        <o:lock v:ext="edit" aspectratio="t"/>
                    </v:shapetype>
                    <v:shape id="_x0000_s1025" type="#_x0000_t75" alt="" style='position:absolute;
                        margin-left:0;margin-top:2pt;width:537pt;height:57pt;z-index:251659264'>
                        <v:imagedata src="http://www.website.com.au/path/to/image.jpg"/>
                        <w:wrap type="square"/>
                    </v:shape>
                    <![endif]-->
                </span>
            </div>
    

    将其放在该部分中:

    <style>
    v\:* {behavior:url(#default#VML);}
    o\:* {behavior:url(#default#VML);}
    w\:* {behavior:url(#default#VML);}
    .shape {behavior:url(#default#VML);}
    

    只需确保所有测量值都以磅为单位。

    【讨论】:

    • 嘿,用了这个例子..效果很好!但我还需要在页脚中添加图像。无法弄清楚如何做到这一点。有什么想法吗?
    【解决方案3】:

    下面的例子:

    • 非常简洁,因为它去除了额外的 HTML 标记,并提供了一个示例,其中大约一半的代码行与其他答案相比
    • 将页眉/页脚元素所需的额外空间降低到 1pt(其他答案会在您的文档中添加几厘米的不可见内容,这通常会导致最后出现额外的空白页)
    • 将页眉/页脚代码定位在内容的顶部,这是一个更好的位置,因为无论内容长度如何,它都不会改变
    • 包含使用页码参考的工作示例(其他一些答案的参考代码已损坏)
    <html>
    <head>
        <style type="text/css">
        @page Section1 {
            margin:0.75in 0.75in 0.75in 0.75in;
            size:841.7pt 595.45pt;
            mso-page-orientation:landscape;
            mso-header-margin:0.5in;
            mso-header: h1;
            mso-footer-margin:0.5in;
            mso-footer: f1;
        }
    
        div.Section1 {page:Section1;}
    
        p.headerFooter { margin:0in; text-align: center; }
        </style>
    </head>
    <body><div class=Section1>
    
    
    <!-- header/footer:
      This element will appears in your main document (unless you save in a separate HTML),
      therefore, we move it off the page (left 50 inches) and relegate its height
      to 1pt by using a table with 1 exact-height row
    -->
    <table style='margin-left:50in;'><tr style='height:1pt;mso-height-rule:exactly'>
        <td>
          <div style='mso-element:header' id=h1>
            <p class=headerFooter>
                Header content goes here
             </p>
          </div>
          &nbsp;
        </td>
    
        <td>
          <div style='mso-element:footer' id=f1>
            <p class=headerFooter>
            Page
            <span style='mso-field-code:PAGE'></span>
            of
            <span style='mso-field-code:NUMPAGES'></span>
          </p>
          </div>
          &nbsp;
    </td></tr></table>
    
    Main content goes here.
    Here's a page break:
    <br clear=all style='mso-special-character:line-break; page-break-before:always'>
    This is page 2
    
    </div></body>
    </html>
    
    

    【讨论】:

    • 感谢 mwag,您的方法帮助很大。如果有人正在寻找一种简单的方法来创建受保护的 word doc,这将有所帮助:在 MS Office 中保护 word doc,在 notepad++ 中保存和编辑文件并查找这些标签:ReadOnlyabc 然后复制这些行并在 之前添加适当的 XML 标记。
    猜你喜欢
    • 2017-05-26
    • 2013-09-18
    • 2013-05-22
    • 2021-07-21
    • 2013-03-09
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多