如何将超链接添加到 Comment 的 InnerText

How to add hyperlink to Comment's InnerText

与我之前的 类似,我正在尝试将超链接添加到当前评论的 InnerText。超链接已成功添加,但当我尝试打开 .docx 文件时,comments.xml 文件格式错误。错误出在 XML 文件中创建的评论段落标记内的超链接标记处。下面的代码有问题吗?

comm.RemoveAllChildren<Paragraph>();
HyperlinkRelationship relation =
                       doc.MainDocumentPart.AddHyperlinkRelationship
                       (new Uri(url, UriKind.RelativeOrAbsolute), true);
                       String relationshipId = relation.Id;

Paragraph paragraph = new Paragraph();
Hyperlink hl = new Hyperlink(
                            new Run(new RunProperties(
                            new RunStyle()
                            {
                                Val = "Hyperlink"
                            }),
                            new Text(currentCommText)
                            ))
                            {
                                History = new DocumentFormat.OpenXml.OnOffValue(true),
                                Id = relationshipId
                            };
paragraph.Append(hl);
comm.Append(paragraph);
cdoc.Comments.Save();
doc.MainDocumentPart.Document.Save();
_________________________________________________________________________________________

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:comments ...>
<w:comment w:initials="d." w:author="User" w:date="2015-08-19T16:45:00Z" w:id="8">
    <w:p>
        <w:hyperlink w:history="true" r:id="R8bd7676b70ad4dad">
            <w:r>
                <w:rPr>
                    <w:rStyle w:val="Hyperlink" />
                </w:rPr>
                <w:t>Text</w:t>
            </w:r>
        </w:hyperlink>
    </w:p>
</w:comment>
...
</w:comments>

我发现了你的问题,是Hyperlink的rId不好(我重新创建了一个docx文档,唯一一次成功重现你的问题是把rId改错了)。

我的猜测是(我没有尝试过,但我认为它会解决问题)不能为评论创建超链接

doc.MainDocumentPart.AddHyperlinkRelationship

但是

doc.MainDocumentPart.WordprocessingCommentsPart.AddHyperlinkRelationship

编辑:文档

快速阅读 https://msdn.microsoft.com/en-us/library/office/cc850832.aspx 了解如何在 Word 文档中插入评论。

如果您查看“.zip”文件夹,您会发现评论在一个单独的文件中 (comments.xml)。因此,当您为您的评论创建 xml 代码并且它引用 WordprocessingCommentsPart 时,在这部分添加超链接删除似乎也是合乎逻辑的