How to insert document page number in a header with word addin?

爷,独闯天下 提交于 2019-12-02 16:21:38

问题


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.");
                });
            });

'

回答1:


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.

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.

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.

JS in ScriptLab

$("#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);
    }
}

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


来源:https://stackoverflow.com/questions/50838137/how-to-insert-document-page-number-in-a-header-with-word-addin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!