Spire.Doc系列教程(26):添加和删除word超链接

Spire.Doc for .NET是一个专业的Word .NET库,设计用于帮助开发人员高效地开发创建、阅读、编写、转换和打印任何来自.NET平台的Word文档文件的功能。本篇文章介绍了如何添加和删除word中的超链接。

更多资源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程

【下载Spire.Doc最新试用版】

Spire.Doc for .NET是一个专业的Word .NET库,设计用于帮助开发人员高效地开发创建、阅读、编写、转换和打印任何来自.NET( C#, VB.NET, ASP.NET)平台的Word文档文件的功能。

本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,本篇文章介绍了如何添加和删除word中的超链接。

超链接指的是在Word文本或者图片中插入能跳转到其他位置或对象的链接,常见的超链接可以链接到 址、电子邮箱地址、外部文件和书签。


C# 添加 Word 超链接

添加文本超链接

//创建一个Document实例并添加sectionDocument doc = new Document();Section section = doc.AddSection();//添加指向 址的超链接Paragraph para1 = section.AddParagraph();para1.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com",HyperlinkType.WebLink);//添加指向邮件地址的超链接Paragraph para2 = section.AddParagraph();para2.AppendHyperlink("mailto:support @ e-iceblue.com", "support @ e-iceblue.com",HyperlinkType.EMailLink);//添加指向外部文件的超链接Paragraph para3 = section.AddParagraph();string filePath = @"C:UsersAdministratorDesktop月销售统计表.xls";para3.AppendHyperlink(filePath, "点击此处打开另一个文档",HyperlinkType.FileLink);//设置段落之间的间距para1.Format.AfterSpacing = 15f;para2.Format.AfterSpacing = 15f;//保存文档doc.SaveToFile("文本超链接.docx", FileFormat.Docx2013);

spire.DOC教程——添加删除超链接

添加图片超链接

//创建一个Document实例并添加sectionDocument doc = new Document();Section section = doc.AddSection();//添加段落Paragraph para = section.AddParagraph();//添加图片到段落并插入 站链接Image image = Image.FromFile(@"C:UsersAdministratorDesktoplogo.png");Spire.Doc.Fields.DocPicture picture = para.AppendPicture(image);para.AppendHyperlink("www.e-iceblue.com", picture, HyperlinkType.WebLink);//保存文档doc.SaveToFile("图片超链接.docx", FileFormat.Docx2013);

spire.DOC教程——添加删除超链接

格式化文本超链接

默认状态下,超文本链接通常显示为蓝色带下划线。为了让超链接更醒目,我们也可以更改超链接的显示形式,例如去掉下划线、改变文本颜色、设置字体大小。

//创建一个Document实例并添加sectionDocument doc = new Document();Section section = doc.AddSection();//添加段落Paragraph para = section.AddParagraph();//添加超链接到段落并返回到一个TextRange对象TextRange txtRange = para.AppendHyperlink("www.e-iceblue.com", "WWW.E-ICEBLUE.COM", HyperlinkType.WebLink);//在TextRange中设置字体名字、大小、颜色、样式txtRange.CharacterFormat.FontName = "黑体";txtRange.CharacterFormat.TextColor = Color.Purple;txtRange.CharacterFormat.FontSize = 15;txtRange.CharacterFormat.Bold = true;txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.None;//保存文档doc.SaveToFile("格式化超链接.docx", FileFormat.Docx2013);

spire.DOC教程——添加删除超链接

C# 删除 Word 超链接

源文档:

spire.DOC教程——添加删除超链接
//创建Word对象并加载文档Document document = new Document();document.LoadFromFile(@"hyperlinks.docx");foreach (Section section in document.Sections){    //删除正文里的超链接    foreach (DocumentObject obj in section.Body.ChildObjects)    {        RemoveLinks(obj,document);    }    //删除页眉页脚中的超链接    foreach (HeaderFooter hf in section.HeadersFooters)    {        foreach (DocumentObject hfobj in hf.ChildObjects)        {            RemoveLinks(hfobj, document);        }    }}//保存文档document.SaveToFile("RemoveLinks.docx",FileFormat.Docx);private static void RemoveLinks(DocumentObject obj,Document document){    //删除段落中的超链接    RemoveLinksInPara(obj,document);    //删除表格中的超链接    if (obj.DocumentObjectType == DocumentObjectType.Table)    {        foreach (TableRow row in (obj as Table).Rows)        {            foreach (TableCell cell in row.Cells)            {                foreach (DocumentObject cobj in cell.ChildObjects)                {                    RemoveLinksInPara(cobj,document);                                                 }            }        }    }}private static void RemoveLinksInPara(DocumentObject obj,Document document)        {    if (obj.DocumentObjectType == DocumentObjectType.Paragraph)    {        var objs = (obj as Paragraph).ChildObjects;        for (int i = 0; i < objs.Count; i++)        {            if (objs[i].DocumentObjectType == DocumentObjectType.Field)            {                //获取超链接域                Field field = objs[i] as Field;                if (field.Type == FieldType.FieldHyperlink)                {                    //获取超链的文本或图片对象                    DocumentObject dObj = field.NextSibling.NextSibling as DocumentObject;                    //删除文本超链接,保留文本和样式                    if (dObj is TextRange)                    {                         //获取超链接文本样式                        CharacterFormat format = (dObj as TextRange).CharacterFormat;                        format.UnderlineStyle = UnderlineStyle.None;                        format.TextColor = Color.Black;                        //创建TextRange并把超链接的文本赋给它                        TextRange tr = new TextRange(document);                        tr.Text = field.FieldText;                        //应用样式                        tr.ApplyCharacterFormat(format);                        //删除文本超链接域                        objs.RemoveAt(i);                        //重新插入文本                        objs.Insert(i, tr);                    }                    //删除图片超链接,保留图片                    if (dObj is DocPicture)                     {                        //删除图片超链接域                        objs.RemoveAt(i);                        //重新插入图片                        objs.Insert(i, dObj);                    }                }            }        }    }}

结果

spire.DOC教程——添加删除超链接

*购买Spire.Doc正版授权的朋友可以点击“咨询在线客服”哦~~

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年6月22日
下一篇 2019年6月22日

相关推荐

发表回复

登录后才能评论