【问题标题】:how to get plain hyperlink text from stored value of ContentControl's Range.WordOpenXML, instead of its syntax value?如何从 ContentControl 的 Range.WordOpenXML 的存储值而不是其语法值中获取纯超链接文本?
【发布时间】:2021-04-09 12:53:09
【问题描述】:

我在 Word 的 ContentControl 中有超链接,如下所示

http://www.yahoo.com

我将它的值存储在下面以供以后使用

var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(cc.Range.WordOpenXML));

当我如下再次解码并获取其文本内容时,

var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(encoded));
XDocument doc = XDocument.Parse(decoded);
string ccText = doc.Descendants(XName.Get("document", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")).FirstOrDefault().Value;

通过这个我得到HYPERLINK "http://www.yahoo.com/" \o "Follow link"而不是http://www.yahoo.com,结果是http://www.yahoo.com

电子邮件也是如此,获取HYPERLINK "mailto:abc@xyz.com" abc@xyz.com 而不是abc@xyz.com

如果我在上述方法中使用cc.Range.WordOpenXML 来获取文本内容,而不是解码的内容,那么我得到的正确值是http://www.yahoo.com

当我将解码后的 XML 与编码前的 XML 进行比较时,似乎 XML 的超链接节点正在被修改,我认为这是导致此问题的根本原因。

编码前的原始XML:取自doc.Descendants(XName.Get("document", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"))

<w:hyperlink r:id="rId4" w:tooltip="Follow link" w:history="1"> 
    <w:r w:rsidRPr="00E862A6">  
      <w:rPr>   
        <w:rStyle w:val="Hyperlink" />  
      </w:rPr>  
      <w:t>http://www.yahoo.com</w:t>   
    </w:r>  
  </w:hyperlink>

解码后更改的 XML 结构:

<w:ins w:id="5" w:author="xxxxxx xxxxxx" w:date="2021-03-30T16:42:00Z">
    <w:r>
      <w:instrText xml:space="preserve"> HYPERLINK "http://www.yahoo.com/" \o "Follow link" </w:instrText>
    </w:r>
    <w:r>
      <w:fldChar w:fldCharType="separate" />
    </w:r>
    <w:r w:rsidRPr="00E862A6">
      <w:rPr>
        <w:rStyle w:val="Hyperlink" />
      </w:rPr>
      <w:t>http://www.yahoo.com</w:t>
    </w:r>
    <w:r>
      <w:rPr>
        <w:rStyle w:val="Hyperlink" />
      </w:rPr>
      <w:fldChar w:fldCharType="end" />
    </w:r>
  </w:ins>

有什么方法可以从 Word 的 ContentControlRange 中获取普通的超链接文本而不是其语法值,就像上面的用例一样存储?不知道我在这里做错了什么。

【问题讨论】:

    标签: c# utf-8 vsto openxml word-contentcontrol


    【解决方案1】:

    我没有为这个根本原因找到任何解决方案,所以直到我找到从没有 HYPERLINK 语法的范围内检索有效所需文本的方法,

    不是最好的方法或完美的解决方案,但作为目前的解决方法,我从字符串中删除了 HYPERLINK \"\\o \"Follow link\",以便在找到其在字符串中的位置后仅获得 http://www.yahoo.com/

    期待实际解决方案。

    【讨论】:

      猜你喜欢
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多