none
在word中定位所有鏈接的位置? RRS feed

答案

  • 這里有個做doc組件的函數庫,推薦給你。下載一個免費版本,再在程序中添加引用(add reference),然後參考以下代碼:

    static void Main(string[] args)
            {
                Document doc = new Document();
                doc.LoadFromFile("Spire.docx");
                List hyperlinks = new List();
                foreach (Section section in doc.Sections)
                {
                    foreach (DocumentObject sec in section.Body.ChildObjects)
                    {
                        if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
                        {
                            foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                            {
                                if (para.DocumentObjectType == DocumentObjectType.Field)
                                {
                                    Field field = para as Field;
    
                                    if (field.Type == FieldType.FieldHyperlink)
                                    {
                                        hyperlinks.Add(field);
                                    }
                                }
                            }
                        }
                    }
                }

    詳細的教程參考這裡

    2015年4月15日 1:52
  • 你好:

    可以尝试一下Word PIA(需要机器上安装Office应用程序),利用HyperLinks属性来获取到各个超链接的位置,也就是Range:

    Application app = new Application();
                app.Visible = true;
                Document doc = app.Documents.Open(@"C:\test.docx");
                Hyperlinks links = doc.Hyperlinks;
                for(int i=1;i<=links.Count;i++)
                {
                    Range r = links[i].Range;
                    string contentToInsert = "[inserted string]";
                    Range newRange = doc.Range(r.End + 1);
                    newRange.InsertAfter(contentToInsert);
                }
    
                doc.Save();
                app.Quit();


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年4月16日 7:19
    版主

全部回复

  • 這里有個做doc組件的函數庫,推薦給你。下載一個免費版本,再在程序中添加引用(add reference),然後參考以下代碼:

    static void Main(string[] args)
            {
                Document doc = new Document();
                doc.LoadFromFile("Spire.docx");
                List hyperlinks = new List();
                foreach (Section section in doc.Sections)
                {
                    foreach (DocumentObject sec in section.Body.ChildObjects)
                    {
                        if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
                        {
                            foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                            {
                                if (para.DocumentObjectType == DocumentObjectType.Field)
                                {
                                    Field field = para as Field;
    
                                    if (field.Type == FieldType.FieldHyperlink)
                                    {
                                        hyperlinks.Add(field);
                                    }
                                }
                            }
                        }
                    }
                }

    詳細的教程參考這裡

    2015年4月15日 1:52
  • 你好:

    可以尝试一下Word PIA(需要机器上安装Office应用程序),利用HyperLinks属性来获取到各个超链接的位置,也就是Range:

    Application app = new Application();
                app.Visible = true;
                Document doc = app.Documents.Open(@"C:\test.docx");
                Hyperlinks links = doc.Hyperlinks;
                for(int i=1;i<=links.Count;i++)
                {
                    Range r = links[i].Range;
                    string contentToInsert = "[inserted string]";
                    Range newRange = doc.Range(r.End + 1);
                    newRange.InsertAfter(contentToInsert);
                }
    
                doc.Save();
                app.Quit();


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年4月16日 7:19
    版主