如何在带有单词addin的标题中插入文档页码? [英] How to insert document page number in a header with word addin?

查看:114
本文介绍了如何在带有单词addin的标题中插入文档页码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何插入页脚和页眉,但是我想知道如何在单词addin中插入页码?

I know how to insert footer and header, but I wonder how can I insert page number with with my word addin?

'Word.run(function (context) {
 var mySections = context.document.sections;
 context.load(mySections, 'body/style');
 return context.sync().then(function () {var myFooter = 
   mySections.items[0].getFooter("primary");
   myFooter.insertParagraph(footerText.value, "End");
                return context.sync().then(function () {
                    console.log("Added a footer to the first section.");
                });
            });

'

推荐答案

我终于找到了时间进行研究并将其整合在一起.这段代码是用ScriptLab编写的.由于ScriptLab抱怨XML代码被分成几行,因此代码片段中的XML都在一行上.为了更好的可读性,我将其粘贴在代码下面的格式化形式中,以便可以查看Word Open XML的结构.

I finally found time to research and put this together. This code is written ScriptLab. Since ScriptLab complains about XML code that's broken into lines, the XML in the code snippet is all on one line. I've pasted it in formatted form below the code for better readability so that it's possible to see how the Word Open XML is structured.

要获取此Word Open XML,我保存了带有PAGE字段的Word文档.然后,按照文章

To get this Word Open XML I saved a Word document with a PAGE field. I then removed all the unnecessary XML, as outlined in the article Create better add-ins for Word with Office Open XML.

请注意,不必在代码中键入XML.也可以将其保存在文件中(请注意文章中有关在文件中引用XML的说明),也可以使用诸如Java的Open XML SDK之类的工具来生成它.

Note that it's not necessary to type the XML into your code. It can also be saved in a file (note the instructions in the article about referencing XML in a file) or generated using a tool such as the Open XML SDK for Javascript.

ScriptLab中的JS

$("#run").click(() => tryCatch(run));

async function run() {
    // Writes a PAGE field to the primary document header
    await Word.run(async (context) => {
        let sXml = '<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"><pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512"><pkg:xmlData><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships></pkg:xmlData></pkg:part><pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"><pkg:xmlData><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText xml:space="preserve"> Page </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r><w:rPr><w:noProof/></w:rPr><w:t>1</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p></w:body></w:document></pkg:xmlData></pkg:part></pkg:package>';

        //console.log("XML: " + sXml);
        let hdr = context.document.sections.getFirst()
            .getHeader("Primary"); //returns Word.Body type
        hdr.insertOoxml(sXml, 'Start');
        await context.sync();
    });
}

/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
    try {
        await callback();
    }
    catch (error) {
        OfficeHelpers.UI.notify(error);
        OfficeHelpers.Utilities.log(error);
    }
}

XML

<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
  <pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
    <pkg:xmlData>
      <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
        <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
      </Relationships>
    </pkg:xmlData>
  </pkg:part>
  <pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
    <pkg:xmlData>
      <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:body>
          <w:p>
            <w:r>
              <w:fldChar w:fldCharType="begin"/>
            </w:r>
            <w:r>
              <w:instrText xml:space="preserve"> Page </w:instrText>
            </w:r>
            <w:r>
              <w:fldChar w:fldCharType="separate"/>
            </w:r>
            <w:r>
              <w:rPr>
                <w:noProof/>
              </w:rPr>
              <w:t>1</w:t>
            </w:r>
            <w:r>
              <w:fldChar w:fldCharType="end"/>
            </w:r>
          </w:p>
        </w:body>
      </w:document>
    </pkg:xmlData>
  </pkg:part>
</pkg:package>

这篇关于如何在带有单词addin的标题中插入文档页码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆