Word控件Spire.Doc 【超链接】教程(5):使用 C# 更改颜色或从 Word 中的超链接中删除下划线

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。

Spire.Doc for.NET 最新下载(qun:767755948)icon-default.png?t=M85Bhttps://www.evget.com/product/3368/download

默认情况下,Word 中的超链接显示为蓝色并带有下划线。在某些情况下,用户可能希望修改超链接样式以使整个文档看起来更好看。本文将介绍如何在 C# 中使用 Spire.Doc 去除下划线或更改超链接的颜色。

代码片段:

第一步:创建一个Document类的新对象,添加一个section。

Document document = new Document();
Section section = document.AddSection();

第 2 步:添加一个段落并将超链接附加到该段落。为了格式化超链接,我们在 TextRange 中返回超链接的值。

Paragraph para= section.AddParagraph();
TextRange txtRange = para1.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);

第 3 步:使用指定的字体名称、字体大小、颜色和下划线样式来格式化超链接。

txtRange.CharacterFormat.FontName = "Times New Roman";
txtRange.CharacterFormat.FontSize = 12;
txtRange.CharacterFormat.TextColor = System.Drawing.Color.Red;
txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.None;

第 4 步:保存文件。

document.SaveToFile("result.docx", FileFormat.Docx2013);

输出

Word控件Spire.Doc 【超链接】教程(5):使用 C# 更改颜色或从 Word 中的超链接中删除下划线_第1张图片

完整代码

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace FormatHyperlink
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();

Paragraph para1= section.AddParagraph();
para1.AppendText("Regular Link: ");
TextRange txtRange1 = para1.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
txtRange1.CharacterFormat.FontName = "Times New Roman";
txtRange1.CharacterFormat.FontSize = 12;
Paragraph blankPara1 = section.AddParagraph();

Paragraph para2 = section.AddParagraph();
para2.AppendText("Change Color: ");
TextRange txtRange2 = para2.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
txtRange2.CharacterFormat.FontName = "Times New Roman";
txtRange2.CharacterFormat.FontSize = 12;
txtRange2.CharacterFormat.TextColor = System.Drawing.Color.Red;
Paragraph blankPara2 = section.AddParagraph();

Paragraph para3 = section.AddParagraph();
para3.AppendText("Remove Underline: ");
TextRange txtRange3 = para3.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
txtRange3.CharacterFormat.FontName = "Times New Roman";
txtRange3.CharacterFormat.FontSize = 12;
txtRange3.CharacterFormat.UnderlineStyle = UnderlineStyle.None;

document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
}
}
}

以上便是如何使用 C# 更改颜色或从 Word 中的超链接中删除下划线,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

 

你可能感兴趣的:(Spire.Doc,for.net,教程(完),word,c#,开发语言,文档管理,spire.doc)