翻譯|使用教程|編輯:吉煒煒|2025-05-15 09:55:13.053|閱讀 143 次
概述:雖然超鏈接可以提供有價值的補充信息,但有時也會分散注意力或造成不必要的困擾,因此可能會需要刪除這些超鏈接。本文將介紹如何使用 Spire.Doc for .NET 通過 C# 刪除 Word 文檔中的超鏈接。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Word 文檔中的超鏈接是可點擊的鏈接,允許讀者導航到一個網站或另一個文檔。雖然超鏈接可以提供有價值的補充信息,但有時也會分散注意力或造成不必要的困擾,因此可能會需要刪除這些超鏈接。本文將介紹如何使用 Spire.Doc for .NET 通過 C# 刪除 Word 文檔中的超鏈接。
Spire.Doc for .NET 是一款功能強大的 .NET 組件,它提供了豐富的 API 來操作 Word 文檔,包括刪除超鏈接。在開始前,您需要先將該.NET Word 庫安裝到您的項目中??梢詮纳舷螺d該組件,然后手動將 DLL 文件作為引用添加到您的 .NET 程序中。也可以直接通過 安裝。
	
PM> Install-Package Spire.Doc
要一次性刪除 Word 文檔中的所有超鏈接,您需要先找到文檔中的所有超鏈接,然后創建自定義方法 FlattenHyperlinks() 將其進行處理扁平化處理以移除超鏈接。步驟如下:
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Collections.Generic;
using Spire.Doc.Fields;
namespace removeWordHyperlink
{
    class Program
    {
        static void Main(string[] args)
        {
            // 創建Document對象
            Document doc = new Document();
            // 加載示例Word文檔
            doc.LoadFromFile("示例.docx");
            // 查找所有超鏈接
            List hyperlinks = FindAllHyperlinks(doc);
            // 扁平化所有超鏈接
            for (int i = hyperlinks.Count - 1; i >= 0; i--)
            {
                FlattenHyperlinks(hyperlinks[i]);
            }
            // 保存結果文件
            doc.SaveToFile("刪除超鏈接.docx", FileFormat.Docx);
        }
        // 創建自定義方法FindAllHyperlinks()來獲取文檔中所有超鏈接
        private static List FindAllHyperlinks(Document document)
        {
            List hyperlinks = new List();
            // 遍歷文檔中每一節的所有元素以查找超鏈接
            foreach (Section section in document.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);
                                }
                            }
                        }
                    }
                }
            }
            return hyperlinks;
        }
        // 創建自定義方法FlattenHyperlinks()來移除所有超鏈接域
        private static void FlattenHyperlinks(Field field)
        {
            int ownerParaIndex = field.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.OwnerParagraph);
            int fieldIndex = field.OwnerParagraph.ChildObjects.IndexOf(field);
            Paragraph sepOwnerPara = field.Separator.OwnerParagraph;
            int sepOwnerParaIndex = field.Separator.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.Separator.OwnerParagraph);
            int sepIndex = field.Separator.OwnerParagraph.ChildObjects.IndexOf(field.Separator);
            int endIndex = field.End.OwnerParagraph.ChildObjects.IndexOf(field.End);
            int endOwnerParaIndex = field.End.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.End.OwnerParagraph);
            FormatFieldResultText(field.Separator.OwnerParagraph.OwnerTextBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex);
            field.End.OwnerParagraph.ChildObjects.RemoveAt(endIndex);
            for (int i = sepOwnerParaIndex; i >= ownerParaIndex; i--)
            {
                if (i == sepOwnerParaIndex && i == ownerParaIndex)
                {
                    for (int j = sepIndex; j >= fieldIndex; j--)
                    {
                        field.OwnerParagraph.ChildObjects.RemoveAt(j);
                    }
                }
                else if (i == ownerParaIndex)
                {
                    for (int j = field.OwnerParagraph.ChildObjects.Count - 1; j >= fieldIndex; j--)
                    {
                        field.OwnerParagraph.ChildObjects.RemoveAt(j);
                    }
                }
                else if (i == sepOwnerParaIndex)
                {
                    for (int j = sepIndex; j >= 0; j--)
                    {
                        sepOwnerPara.ChildObjects.RemoveAt(j);
                    }
                }
                else
                {
                    field.OwnerParagraph.OwnerTextBody.ChildObjects.RemoveAt(i);
                }
            }
        }
        // 創建自定義方法FormatFieldResultText()對超鏈接文本進行格式設置
        private static void FormatFieldResultText(Body ownerBody, int sepOwnerParaIndex, int endOwnerParaIndex, int sepIndex, int endIndex)
        {
            for (int i = sepOwnerParaIndex; i <= endOwnerParaIndex; i++)
            {
                Paragraph para = ownerBody.ChildObjects[i] as Paragraph;
                if (i == sepOwnerParaIndex && i == endOwnerParaIndex)
                {
                    for (int j = sepIndex + 1; j < endIndex; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
                else if (i == sepOwnerParaIndex)
                {
                    for (int j = sepIndex + 1; j < para.ChildObjects.Count; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
                else if (i == endOwnerParaIndex)
                {
                    for (int j = 0; j < endIndex; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
                else
                {
                    for (int j = 0; j < para.ChildObjects.Count; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
            }
        }
        // 創建自定義方法FormatText()將超鏈接文本的字體顏色設置為黑色并移除下劃線
        private static void FormatText(TextRange tr)
        {
            //Set the text color to black
            tr.CharacterFormat.TextColor = Color.Black;
            //Set the text underline style to none
            tr.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
        }
    }
}
	 
 
使用 Spire.Doc for .NET 庫,您能快速定位并刪除 Word 文檔中的超鏈接,同時保留原始文本內容。通過消除不相關的鏈接,您可以提高文檔質量、改善用戶體驗。Spire.Doc for .NET 庫直觀的 API 和高效的性能使其成為企業應用程序的理想選擇。
————————————————————————————————————————
關于慧都科技:
慧都科技是一家行業數字化解決方案公司,長期專注于軟件、油氣與制造行業。公司基于深入的業務理解與管理洞察,以系統化的業務建模驅動技術落地,幫助企業實現智能化運營與長期競爭優勢。在軟件工程領域,我們提供開發控件、研發管理、代碼開發、部署運維等軟件開發全鏈路所需的產品,提供正版授權采購、技術選型、個性化維保等服務,幫助客戶實現技術合規、降本增效與風險可控。慧都科技E-iceblue的官方授權代理商,提供E-iceblue系列產品免費試用,咨詢,正版銷售等于一體的專業化服務。E-iceblue旗下Spire系列產品是國產文檔處理領域的優秀產品,支持國產化,幫助企業高效構建文檔處理的應用程序。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(125237868)
	
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:慧都網