如何使用 OOXML 在 MS Word 中添加自定义注释?

问题描述

我正在创建一个单词自定义加载项。在该用户可以添加带有颜色、粗体或突出显示的自定义评论或设置其他可能的选项。但是当我尝试直接使用 <b>Hello This is comment</b> 并尝试使用以下选项时。当第一个选项是尝试时,它给了我 RichApi.Error GeneralException: GeneralException. 当我尝试将其写为评论时,它会按原样打印。

{\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}{\f1\fswiss\fprq2\fcharset0 Microsoft Sans Serif;}{\f2\fnil\fcharset2 符号;}}\r\n{\colortbl ;\red0\green0\blue255;\red5\green99\blue193;}\r\n{\\generator Riched20 10.0.17763}\viewkind4\uc1\r\n\pard\f0\fs17\lang1033 使用括号引用样式,大多数版本如下 格式:\par\r\n\par\r\n\r\n\pard{\pntext\f2\'B7\tab}{\\pn\pnlvlblt\pnf2\pnindent0{\pntxtb\' B7}}... (史密斯,2005)。\par\r\n{\pntext\f2\'B7\tab}史密斯(2005)陈述 ...\par\r\n\r\n\pard\par\r\n哈佛系统可能会有所不同,所以请 检查您机构的风格指南以确认。 \par\r\n\par\r\n\r\n\pard\widctlpar {\f1\fs16{\field{\*\fldinst{超链接 "https://mysitehere"}}{\fldrslt{\ul\cf1\cf2\ul\lang2057 单击此处}}}}\f1\fs16 了解更多 信息。\f0\fs17\par\r\n}\r\n

我的代码:

range.insertOoxml(
    '<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/_rels/document.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="256"><pkg:xmlData><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="comments.xml" xmlns="http://schemas.openxmlformats.org/package/2006/relationships" /></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:commentRangeStart w:id="0"/><w:r><w:t>' + selectedText + '</w:t></w:r><w:commentRangeEnd w:id="0"/><w:r><w:commentReference w:id="0"/></w:r></w:p></w:body></w:document></pkg:xmlData></pkg:part><pkg:part xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" pkg:name="/word/comments.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"><pkg:xmlData><w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:comment xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:id="0"><w:sdtContent><w:p><w:r><w:t>' + commentMessage + '</w:t></w:r></w:p></w:sdtContent></w:comment></w:comments></pkg:xmlData></pkg:part><pkg:part pkg:name="/word/_rels/comments.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml"><pkg:xmlData><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships></pkg:xmlData></pkg:part></pkg:package>',"Replace"
);

如果有人对此有任何想法,请告诉我。

解决方法

您应该能够使用 Ooxml 自定义您要插入的注释,只需确保使用正确的样式格式来设置注释的样式。

从此更改 Ooxml 的以下部分:

<w:comment xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:id="0">
  <w:sdtContent>
    <w:p>
      <w:r>
        <w:t>' + commentMessage + '</w:t>
      </w:r>
    </w:p>
  </w:sdtContent>
</w:comment>

为此:

<w:comment xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:id="0">
  <w:sdtContent>
    <w:p>
      <w:r>
        <w:t xml:space="preserve">This is a </w:t>
      </w:r>
      <w:r>
        <w:rPr>
          <w:b/>
          <w:bCs/>
        </w:rPr>
        <w:t>customized</w:t>
      </w:r>
      <w:r>
        <w:t xml:space="preserve"> comment.</w:t>
      </w:r>
    </w:p>
  </w:sdtContent>
</w:comment>

请注意,“自定义”一词现在将在评论中以粗体显示。

如果您想添加更多格式,例如突出显示,这里是另一个示例:

<w:r>
  <w:t xml:space="preserve">This comment is </w:t>
</w:r>
<w:r>
  <w:rPr>
    <w:highlight w:val="yellow"/>
  </w:rPr>
  <w:t>highlighted</w:t>
</w:r>

您可以详细了解 Office Open XML 样式 here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...