翻譯|使用教程|編輯:胡濤|2022-11-22 10:35:24.813|閱讀 162 次
概述:本文將重點(diǎn)演示如何在 C# 中刪除 word 文檔中的所有超鏈接。歡迎查閱~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對(duì) Word 文檔進(jìn)行操作的 .NET 類庫(kù)。在于幫助開(kāi)發(fā)人員無(wú)需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開(kāi)發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開(kāi)發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
借助Spire.Doc,我們可以很方便的在word文檔中添加超鏈接和編輯超鏈接。本文將重點(diǎn)演示如何在 C# 中刪除 word 文檔中的所有超鏈接。
下面就詳細(xì)介紹如何去除word文檔中的超鏈接。首先,查看帶有許多超鏈接的示例文檔:
第 1 步:創(chuàng)建一個(gè)新的 word 文檔并從文件中加載示例文檔:
Document doc = new Document(); doc.LoadFromFile("Sample.docx");
第 2 步:創(chuàng)建一個(gè)方法 FindAllHyperlinks() 以獲取示例文檔中的所有超鏈接。
private 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; }
第 3 步:刪除超鏈接的字體顏色和下劃線格式。
private 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); } } } } private void FormatText(TextRange tr) { tr.CharacterFormat.TextColor = Color.Black; tr.CharacterFormat.UnderlineStyle = UnderlineStyle.None; }
第 4 步:展平超鏈接字段。
private 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); } } }
第 5 步:將文檔保存到文件
doc.SaveToFile("removehyperlinks.docx", FileFormat.Docx);
去除word文檔超鏈接后的有效截圖:
如何從word文檔中刪除超鏈接的完整代碼:
public Removehyperlink() { Document doc = new Document(); doc.LoadFromFile("Sample.docx"); List hyperlinks = FindAllHyperlinks(doc); for (int i = hyperlinks.Count - 1; i >= 0; i--) { FlattenHyperlinks(hyperlinks[i]); } doc.SaveToFile("removehyperlinks.docx", FileFormat.Docx); } private 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; } private 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); } } } private 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); } } } } private void FormatText(TextRange tr) { tr.CharacterFormat.TextColor = Color.Black; tr.CharacterFormat.UnderlineStyle = UnderlineStyle.None; }
以上便是如何在C#去除word文檔中的超鏈接,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn