简体   繁体   中英

HTML to MS Word doc with total number of pages

I am trying to create a Word document using HTML.

Thanks to a number of examples I have been able to acheive this. However, I can't seem to place the total number of pages in text at the top of the first page in the document.

I am trying to add, "This document contains "PAGENUM" of pages in the first line of text, but when the document is created, the "PAGENUM" is blank (other than in the footer that I also would like to keep).

Is what I'm trying to achieve even possible or am I just getting it completely wrong?

My code (Thanks to another user Vahid) is as follows,

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

 This document contains <span style='mso-field-code: NUMPAGES '></span> pages.

 MORE CONTENT

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

Thanks in advance.

If you try to do it in Word and clean a bit the code it generates, you get something like this:

            ...
<div class="Section1">

 This document contains
 <!--[if supportFields]>
  <span>
   <span style='mso-element:field-begin'></span>
   <span style='mso-spacerun:yes'> </span>
   NUMPAGES
   <span style='mso-spacerun:yes'>  </span>
   \* MERGEFORMAT <span style='mso-element:field-separator'></span>
  </span>
 <![endif]-->
 <span style='mso-no-proof:yes'>X</span>
 <!--[if supportFields]>
  <span>
   <span style='mso-element:field-end'></span>
  </span>
 <![endif]--> pages.

 MORE CONTENT
        ...

Please note that when you open the Word document, you have to "Update field" to see the correct number of pages. Otherwise it will display the "mso-no-proof" value (here "X").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM