如何在带有单词addin的标题中插入文档页码?

时间:2018-06-13 13:10:11

标签: ms-word office-js office-addins word-addins

我知道如何插入页脚和页眉,但我想知道如何插入页码和我的单词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.");
                });
            });

'

1 个答案:

答案 0 :(得分:0)

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

要获取此Word Open XML,我保存了带有PAGE字段的Word文档。然后,我删除了所有不必要的XML,如文章Create better add-ins for Word with Office Open XML所述。

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

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>