如何在Comment的InnerText中添加超链接

时间:2015-08-21 10:37:21

标签: c# hyperlink openxml

与我之前的question类似,我正在尝试添加一个指向当前评论的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>

1 个答案:

答案 0 :(得分:1)

我发现了你的问题,这是超级链接的不好之处(我重新创建了一个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时,在此部分中添加超链接也是合乎逻辑的