php - HTML 到 MS Word 文档的总页数
问题描述
我正在尝试使用 HTML 创建 Word 文档。
多亏了一些例子,我才能够做到这一点。但是,我似乎无法将文本中的总页数放在文档第一页的顶部。
我正在尝试添加,“该文档在第一行文本中包含“PAGENUM”页,但是在创建文档时,“PAGENUM”为空白(除了我也想保留的页脚)。
我正在努力实现的目标是可能的,还是我完全错了?
我的代码(感谢另一个用户 Vahid)如下,
<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>
提前致谢。
解决方案
如果您尝试在 Word 中执行此操作并清理一下它生成的代码,您会得到如下内容:
...
<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
...
请注意,当您打开 Word 文档时,您必须“更新字段”才能看到正确的页数。否则它将显示“mso-no-proof”值(此处为“X”)。
推荐阅读
- javascript - 在 React Native 中将卡片标题与图像垂直对齐
- powershell - 使用 Powershell 删除多个 csv 文件中的空行
- php - PHP将数组的值更改为字符
- visual-studio-code - VS Code 在一个 WSL 中有效,但在其他 WSL 中无效
- r - 如何使用 highcharter 库进行双轴操作?
- django - 如何在 django-graphql-jwt 中集成 Facebook 登录?
- asp.net - 在 https://login.microsoftonline.com 访问 XMLHttpRequest 已被 CORS 策略阻止
- apache-spark - 当我在 pyspark 中进行缓存时,数据连接是否需要重新分区
- asp.net-mvc - 在应用启动时从 IHostedService 异步更新 autofac 容器
- xcode - XCode Build 失败并出现 rsync 错误:“rsync 错误:收到 SIGINT、SIGTERM 或 SIGHUP”