持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第4天,点击查看活动详情
前言:
今天这篇文章我们来学习一下使用C#对Office软件中的Word文档操作,我们通过添加Spire.Doc依赖类对Word文件进行插入、格式化、删除Word的超链接,今天这篇文章我们就一起来学习一下吧,创作不易,大家顺便点点赞吧,你的点赞收藏关注,是我写文章的动力,栓Q啦。
导入Spire.Doc依赖
我们导入Spire.Doc依赖,这样才能使用下面的代码
超链接
超链接简单来讲就是内容链接,通过设置超链接可以实现对象与网页、站点之间的连接。链接目标可以是网页、图片、邮件地址、文件夹或者是应用程序。设置链接的对象可以是文本或者图片。在下面的示例中,将讲述如何通过使用类库来添加Word超链接。同理,我们也可以格式化超链接,例如,设置超链接文本颜色,下划线,链接地址等,也可以删除文档中已经存在的一些超链接,例如:页眉处的链接、正文段落中的链接、表格中的链接、图片中的链接。以上操作我们都可以通过借助下面的类库来实现。
添加文本链接
命名空间,可复制,也可alt+Enter加载
using System;
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;
创建文档
//创建一个Document实例并添加section
Document doc = new Document();
Section section = doc.AddSection();
根据需要设置链接到不同对象的超链接
//添加指向网址的超链接
Paragraph para1 = section.AddParagraph();
para1.AppendHyperlink("www.google.com", "www.google.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:\Users\Administrator\Desktop\2017NobelPrize.docx";
para3.AppendHyperlink(filePath, "点击打开文档", HyperlinkType.FileLink);
设置段间距
para1.Format.AfterSpacing = 15f;
para2.Format.AfterSpacing = 15f;
保存文件
doc.SaveToFile("文本超链接.docx", FileFormat.Docx2013);
完成代码后,调试运行程序,生成稳定
全部代码如下:
using System;
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;
namespace Insert_Word
{
class Program
{
static void Main(string[] args)
{
//创建一个Document实例并添加section
Document doc = new Document();
Section section = doc.AddSection();
//添加指向网址的超链接
Paragraph para1 = section.AddParagraph();
para1.AppendHyperlink("www.google.com", "www.google.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:\Users\Administrator\Desktop\2017NobelPrize.docx";
para3.AppendHyperlink(filePath, "点击打开文档", HyperlinkType.FileLink);
//设置段落之间的间距
para1.Format.AfterSpacing = 15f;
para2.Format.AfterSpacing = 15f;
//保存文档
doc.SaveToFile("文本超链接.docx", FileFormat.Docx2013);
}
}
}
添加图片链接
命名空间,可复制,也可alt+Enter加载
using System;
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;
创建文档
Document doc = new Document();
Section section = doc.AddSection();
Paragraph para = section.AddParagraph();
添加链接到图片
//添加图片到段落并插入网站链接
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\images\Google.jpg");
Spire.Doc.Fields.DocPicture picture = para.AppendPicture(image);
para.AppendHyperlink("www.google.com", picture, HyperlinkType.WebLink);
保存文档
doc.SaveToFile("图片超链接.docx", FileFormat.Docx2013);
设置超链接格式
一般情况下,对文本设置超链接都是默认的蓝色字体,带有下划线,在下面的操作中,我们可以自行设置超链接的文本字体、字号、颜色、下划线等。
代码逻辑
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace FormatHyperlink
{
class Program
{
static void Main(string[] args)
{
//初始化一个Docuemtn类对象,并添加section
Document document = new Document();
Section section = document.AddSection();
//添加段落,并设置超链接文本和链接网址。设置字体、字号、字体颜色、下划线等。
Paragraph para = section.AddParagraph();
para.AppendText("HyperLink: ");
TextRange txtRange = para.AppendHyperlink("www.google.com", "www.google.com", HyperlinkType.WebLink);
txtRange.CharacterFormat.FontName = "Times New Roman";
txtRange.CharacterFormat.FontSize = 14;
txtRange.CharacterFormat.TextColor = System.Drawing.Color.Green;
txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
//保存并打开文档
document.SaveToFile("result1.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result1.docx");
}
}
}
删除超链接
下面的测试文档中,多处文档内容包含超链接,包括页眉处的文字超链接、正文段落中的文字超链接、表格中的图片超链接等,可通过下面的代码将超链接删除。
测试文档:
代码逻辑:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
using System.Drawing;
namespace RemoveHyperlink_Doc
{
class Program
{
static void Main(string[] args)
{
//创建Word对象并加载文档
Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
//遍历文档中所有section
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);
System.Diagnostics.Process.Start("RemoveLinks.docx");
}
//自定义方法RemoveLinks()删除段落、表格中的超链接
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);
}
}
}
}
}
//自定义方法RemoveLinksInPara()删除文档段落中的所有超链接
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
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);
}
}
}
}
}
}
}
}
总结
这篇文章比较简单,只是简单的学习一下,对它有更多的认识,在有需求的时候最起码有路子,虽然很简单,但是也是可以学到东西的,我们学习了新的知识,对我们的知识储备及技术又有新的一点点的进步,C#的技术就是先简单再难嘛,积少成多之后才会成长才会进步,我们要不断的学习不断的探索,才能有学习的动力,才会有学习的欲望,东西不多,但是实用性挺强建议收藏一下,哈哈哈哈,如果你觉得这篇文章对你有用,点赞最实在,创作不易,点赞评论收藏关注,嘿嘿,不喜勿喷!!!!