2012-02-18 86 views
5

我正在生成“Word文档”,并在第一页上将标题作为HTML代码生成。 <style> 标签包含:Office HTML Word标题

@page Section { 
    size:8.5in 11.0in; 
    margin: 0.7in 0.9in 0.7in 0.9in; 
    mso-header-margin: 0.0in; 
    mso-footer-margin: 0.0in; 
    mso-title-page: yes; 
    mso-first-header: fh1; 
    mso-paper-source: 0; 
} 
div.Section { 
    page: Section; 
} 

<body>

<div class="Section" style="font-family: Verdana, sans-serif;"> 
    <![if supportFields]> 
     <div style="mso-element:header" id="fh1"> 
      <p class="MsoHeader"> 
       header 
      </p> 
     </div> 
    <![endif]> 
</div> 

这表明无论是在头部,并在文件的主体的 “标题” 文本。资源(http://techsynapse.blogspot.com/2007/03/generating-word-document-dynamically.html和http://www.pbdr.com/ostips/wordfoot.htm)我发现,如果没有使用额外的头文件是没有办法避免这种影响不是我的选择。

这里有我的问题:你知道任何解决这个问题的方法吗?如何隐藏主文档中标题的出现位置,但将其保留在页面顶部的位置?预先感谢您的任何建议。

回答

7

这为我工作:

<html 
xmlns:o='urn:schemas-microsoft-com:office:office' 
xmlns:w='urn:schemas-microsoft-com:office:word' 
xmlns='http://www.w3.org/TR/REC-html40'> 
<head><title></title> 

<!--[if gte mso 9]><xml> 
<w:WordDocument> 
    <w:View>Print</w:View> 
    <w:Zoom>90</w:Zoom> 
</w:WordDocument> 
</xml><![endif]--> 


<style> 
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> 

<!-- /* Style Definitions */ 

@page Section1 
{ 
    size:8.5in 11.0in; 
    margin:1.0in 1.0in 1.0in 1.0in; 
    mso-header-margin:.5in; 
    mso-footer-margin:.5in; 
    mso-title-page:yes; 
    mso-header: h1; 
    mso-footer: f1; 
    mso-first-header: fh1; 
    mso-first-footer: ff1; 
    mso-paper-source:0; 
} 


div.Section1 
{ 
    page:Section1; 
} 

table#hrdftrtbl 
{ 
    margin:0in 0in 0in 900in; 
    width:1px; 
    height:1px; 
    overflow:hidden; 
} 
--> 
</style></head> 

<body lang=EN-US style='tab-interval:.5in'> 
<div class=Section1> 

<p> CONTENT </p> 

<br/> 
    <table id='hrdftrtbl' border='0' cellspacing='0' cellpadding='0'> 
    <tr><td> 

    <div style='mso-element:header' id=h1 > 
     <p class=MsoHeader ><p>&nbsp;HEADER-TITLE</p></p> 
    </div> 

    </td> 
    <td> 
    <div style='mso-element:footer' id=f1> 

     <p>&nbsp;FOOTER-TITLE</p> 
     <p class=MsoFooter> 
     <span style=mso-tab-count:2'></span> 
      Page <span style='mso-field-code: PAGE '><span style='mso-no-proof:yes'></span></span> of <span style='mso-field-code: NUMPAGES '></span> 
     </p> 

    </div> 



    <div style='mso-element:header' id=fh1> 

    <p class=MsoHeader><span lang=EN-US style='mso-ansi-language:EN-US'>&nbsp;FIRST-HEADER-TITLE<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;FIRST-FOOTER-TITLE<o:p></o:p></span></p> 

    </div> 

    </td></tr> 
    </table> 


</body></html> 
+0

它总是为我显示一个额外的空间。 – Ryan 2012-10-16 21:59:50

+1

无法从服务器生成头中的图像,但在本地主机上工作正常。为图像生成的路径在服务器上也很好。你能告诉我什么我错过了本地主机上的图像被添加到word文件中,但在服务器上没有。 **注意**在本地机器上,我安装了ms-office,但是不在服务器上。 – 2013-04-30 13:15:11

+0

如何删除标题部分的空白空间?当我在单词中查看它时,一切正常,但打印时会添加空白空间......任何想法? – 2013-08-06 09:50:58

3

要在上面的回答通过vencedor更新和删除添加的额外空字符编码到您的页眉/页脚图像/文本,你可以做这样的事情:

<html 
xmlns:o='urn:schemas-microsoft-com:office:office' 
xmlns:w='urn:schemas-microsoft-com:office:word' 
xmlns='http://www.w3.org/TR/REC-html40'> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title></title> 

<!--[if gte mso 9]><xml> 
<w:WordDocument> 
    <w:View>Print</w:View> 
    <w:Zoom>100</w:Zoom> 
</w:WordDocument> 
</xml><![endif]--> 
<style> 
<!-- /* Style Definitions */ 
p.MsoHeader, li.MsoHeader, div.MsoHeader{ 
    margin:0in; 
    margin-top:.0001pt; 
    mso-pagination:widow-orphan; 
    tab-stops:center 3.0in right 6.0in; 
} 
p.MsoFooter, li.MsoFooter, div.MsoFooter{ 
    margin:0in 0in 1in 0in; 
    margin-bottom:.0001pt; 
    mso-pagination:widow-orphan; 
    tab-stops:center 3.0in right 6.0in; 
} 
.footer { 
    font-size: 9pt; 
} 
@page Section1{ 
    size:8.5in 11.0in; 
    margin:0.5in 0.5in 0.5in 0.5in; 
    mso-header-margin:0.5in; 
    mso-header:h1; 
    mso-footer:f1; 
    mso-footer-margin:0.5in; 
    mso-paper-source:0; 
} 
div.Section1{ 
    page:Section1; 
} 
table#hrdftrtbl{ 
    margin:0in 0in 0in 9in; 
} 
--> 
</style> 
<style type="text/css" media="screen,print"> 
body { 
    font-family: "Calibri", "Verdana","HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 
    font-size:12pt; 
} 
pageBreak { 
    clear:all; 
    page-break-before:always; 
    mso-special-character:line-break; 
} 
</style> 
</head> 
<body style='tab-interval:.5in'> 
<div class="Section1"> 

Something page 1 
<div class="pageBreak"></div> 
Something page 2 
<div class="pageBreak"></div> 

<table id='hrdftrtbl' border='1' cellspacing='0' cellpadding='0'> 
     <tr> 
      <td> 
       <div style='mso-element:header' id="h1" > 
        <p class="MsoHeader"> 
         <table border="0" width="100%"> 
          <tr> 
           <td> 

            YOUR_HEADER IMAGE/TEXT 

           </td> 
          </tr> 
         </table> 
        </p> 
       </div> 
      </td> 
      <td> 
      <div style='mso-element:footer' id="f1"> 
       <p class="MsoFooter"> 
        <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
         <tr> 
          <td align="center" class="footer"> 
          YOUR_FOOTER_TEXT 
           <g:message code="offer.letter.page.label"/> <span style='mso-field-code: PAGE '></span> of <span style='mso-field-code: NUMPAGES '></span> 
          </td> 
         </tr> 
        </table> 
       </p> 
      </div> 
     </td> 
    </tr> 
    </table> 
    </div> 
</body> 
</html> 

这现在包括页眉和页脚段的完整CSS组件。

实际上删除额外编码的技巧是在MsoFooter和MsoHeader部分中声明另一个表。

+0

非常有用,谢谢! 您是否知道是否有任何可能允许正文文本在标题部分重叠的属性? 因为它现在正文文本总是在标题的最后一行之下。 – 2016-02-02 13:31:09

+1

看看这里http://sebsauvage.net/wiki/doku.php?id=word_document_generation。我们已经结束了使用mhtml模型(在最底部),它完美的工作。这个当前方法的问题是隐藏表,在我们的情况下,当最后一页大于一半时创建额外页面。 – Vahid 2016-02-02 13:56:05

+0

是的,这正是我已经成功使用的教程。但我没有设法让文本与标题重叠。另一种选择是使用水印而不是头部,但这意味着将头部html转换为图片,这并不容易:/ 因此,如果您有一个技巧让正文文本从页面顶部开始,甚至如果有一个标题,那就太棒了:) – 2016-02-02 13:59:28

相关问题