翻译|使用教程|编辑:吉炜炜|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 库安装到您的项目中。可以从上下载该组件,然后手动将 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
文章转载自:慧都网