Офисный HTML-заголовок Word - PullRequest
       20

Офисный HTML-заголовок Word

5 голосов
/ 18 февраля 2012

Я генерирую «документ Word» с заголовком на первой странице в виде HTML-кода.Тег <style> содержит:

@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), которые я обнаружил, говорят, что невозможно избежать этого эффекта без использования дополнительного заголовочного файла, который мне не подходит.

вопрос: знаете ли вы какие-либо обходные пути для этой проблемы? Как я могу скрыть вхождение заголовка в основном документе, но оставить его на своем месте вверху страницы? Заранее благодарим за любой совет.

Ответы [ 2 ]

7 голосов
/ 05 сентября 2012

Это сработало для меня:

<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>
5 голосов
/ 24 декабря 2015

Чтобы обновить ответ выше 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.

...