从字处理文档中检索注释

本主题介绍如何使用 Open XML SDK for Office 中的类以编程方式检索字处理文档中main文档部件的注释。


打开现有文档进行只读访问

若要打开现有文档,请实例化 类, WordprocessingDocument 如以下 using 语句所示。 在同一语句中,使用 Open(String, Boolean, OpenSettings) 方法在指定的 fileName 处打开字处理文件。 若要打开文件以编辑布尔参数,请将 设置为 true。 在此示例中,只需读取文件;因此,可以通过将布尔参数设置为 false来打开文件进行只读访问。

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, false))
{
    if (wordDoc.MainDocumentPart is null || wordDoc.MainDocumentPart.WordprocessingCommentsPart is null)
    {
        throw new System.ArgumentNullException("MainDocumentPart and/or WordprocessingCommentsPart is null.");
    }

在 v3.0.0+ 中, Close() 已删除 方法,转而依赖于 using 语句。 它确保在 Dispose() 到达右大括号时自动调用 方法。 using 语句后面的块为 using 语句中创建或指定的对象设定范围。 WordprocessingDocument由于 Open XML SDK 中的 类会自动保存并关闭对象作为其IDisposable实现的一using部分,并且由于Dispose()在退出块时会自动调用,因此无需显式调用 Save()Dispose() ,只要使用 语句。


Comments 元素

commentscomment 元素对于处理字处理文件中的注释至关重要。 在此代码示例中,熟悉这些元素非常重要。

ISO/IEC 29500(该链接可能指向英文页面) 规范中的以下信息介绍了 comments 元素。

comments(注释集合)

此元素指定当前文档中定义的所有注释。 它是 WordprocessingML 文档的注释部件的根元素。

考虑以下 WordprocessingML 片段,它表示 WordprocessingML 文档中注释部件的内容:

    <w:comments>
      <w:comment … >
        …
      </w:comment>
    </w:comments>

© ISO/IEC 29500:2016

以下 XML 架构段定义 Comments 元素的内容。

    <complexType name="CT_Comments">
       <sequence>
           <element name="comment" type="CT_Comment" minOccurs="0" maxOccurs="unbounded"/>
       </sequence>
    </complexType>

Comment 元素

ISO/IEC 29500(该链接可能指向英文页面) 规范中的以下信息介绍了 comment 元素。

comment(注释内容)

此元素指定存储在 WordprocessingML 文档的注释部件中的单个注释的内容。

如果注释未通过有效使用的 commentReference 元素的匹配 id 属性由文档内容引用,则加载文档时可能会忽略该注释。 如果多个注释共享同一 id 属性值,则应该仅加载一个注释,其他注释可能会被忽略。

请考虑所含文本带有批注注释的文档,如下所示:

具有批注注释的文档文本

此注释由以下 WordprocessingML 片段表示。

    <w:comment w:id="1" w:initials="User">
      …
    </w:comment>

comment 元素指定在注释部件中存在一个注释。

© ISO/IEC 29500:2016

以下 XML 架构段定义 comment 元素的内容。

    <complexType name="CT_Comment">
       <complexContent>
           <extension base="CT_TrackChange">
              <sequence>
                  <group ref="EG_BlockLevelElts" minOccurs="0" maxOccurs="unbounded"/>
              </sequence>
              <attribute name="initials" type="ST_String" use="optional"/>
           </extension>
       </complexContent>
    </complexType>

示例代码的工作方式

打开文件进行只读访问后,可以实例化 WordprocessingCommentsPart 类。 然后,可以显示元素的内部文本 Comment

WordprocessingCommentsPart commentsPart = wordDoc.MainDocumentPart.WordprocessingCommentsPart;

if (commentsPart is not null && commentsPart.Comments is not null)
{
    foreach (Comment comment in commentsPart.Comments.Elements<Comment>())
    {
        Console.WriteLine(comment.InnerText);
    }
}

示例代码

以下是使用 C# 和 Visual Basic 编写的完整示例代码。

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;

static void GetCommentsFromDocument(string fileName)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, false))
    {
        if (wordDoc.MainDocumentPart is null || wordDoc.MainDocumentPart.WordprocessingCommentsPart is null)
        {
            throw new System.ArgumentNullException("MainDocumentPart and/or WordprocessingCommentsPart is null.");
        }

        WordprocessingCommentsPart commentsPart = wordDoc.MainDocumentPart.WordprocessingCommentsPart;

        if (commentsPart is not null && commentsPart.Comments is not null)
        {
            foreach (Comment comment in commentsPart.Comments.Elements<Comment>())
            {
                Console.WriteLine(comment.InnerText);
            }
        }
    }
}

另请参阅