简体   繁体   English

如何使用OpenXML SDK获取MS Word中超链接的绝对URI?

[英]How to get absolute URI of hyperlink in MS word using OpenXML SDK?

A MS word file contains a link to local files (MS word, excel, visio ...), I used OpenXML to get absolute URI of linking local files. 一个MS word文件包含一个到本地文件的链接(MS word,excel,visio ...),我使用OpenXML来获取链接本地文件的绝对URI。 But I only get filename. 但是我只得到文件名。

Could you give me some advice? 你能给我一些建议吗?

  using (WordprocessingDocument document = WordprocessingDocument.Open(docxFile, true))
{
    Body body = document.MainDocumentPart.Document.Body;

    //-----------------------------------------------
    DocumentFormat.OpenXml.Wordprocessing.Hyperlink hLink = body.Descendants<DocumentFormat.OpenXml.Wordprocessing.Hyperlink>().FirstOrDefault();
    if (hLink != null)
    {
        // get hyperlink's relation Id (where path stores)
        string relationId = hLink.Id;
        if (relationId != string.Empty)
        {
            // get current relation
            HyperlinkRelationship hr = document.MainDocumentPart.HyperlinkRelationships.Where(a => a.Id == relationId).FirstOrDefault();
            if (hr != null)
            { 
                string path = hr.Uri.AbsolutePath; //exception
            }
        }
    }
}

just use hr.Uri.ToString() method to get the path 只需使用hr.Uri.ToString()方法获取路径

According to MSDN: "The AbsolutePath property contains the path information that the server uses to resolve requests for information." 根据MSDN:“ AbsolutePath属性包含服务器用来解析信息请求的路径信息。”

https://msdn.microsoft.com/en-us/library/system.uri.absolutepath%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 https://msdn.microsoft.com/zh-cn/library/system.uri.absolutepath%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM