在 Word 中应用强调标记(C#/VB.NET)
Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。下面我们将给您介绍如何在 Word 中应用强调标记。
Word 文档中使用强调标记来强调单词并使它们更加引人注目。它通常是放在强调词上方或下方的一个点或一个圆圈。但是,手动选择单词并在其上应用强调标记需要大量工作。幸运的是,用于 .NET 的 Spire.Doc提供了一种更简单的方法来通过代码应用强调标记。本文将向您展示如何使用 Spire.Doc for .NET将强调标记应用于 Word 文档中的文本。
为 .NET 安装 Spire.Doc
首先,您需要将 Spire.Doc for.NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。
PM> Install-Package Spire.Doc
将强调标记应用于指定文本
详细步骤如下:
- 创建一个文档实例。
- 使用Document.LoadFromFile()方法从磁盘加载 Word 文档。
- 使用Document.FindAllString()方法查找您需要强调的文本。
- 通过CharacterFormat.EmphasisMark属性将强调标记应用于找到的文本。
- 使用Document.SaveToFile()方法将文档保存到另一个 Word 文件。
【C#】
using System;
using Spire.Doc;
using Spire.Doc.Documents;
namespace applyemphasismark
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load the Word document from disk
document.LoadFromFile(@"D:\testp\test.docx");
//Find text you want to emphasize
TextSelection[] textSelections = document.FindAllString("Spire.Doc for .NET", false, true);
//Apply emphasis mark to the found text
foreach (TextSelection selection in textSelections)
{
selection.GetAsOneRange().CharacterFormat.EmphasisMark = Emphasis.Dot;
}
//Save the document to another Word file
string output = "ApllyEmphasisMark.docx";
document.SaveToFile(output, FileFormat.Docx);
}
}
}
【VB.NET】
using System;
using Spire.Doc;
using Spire.Doc.Documents;
namespace applyemphasismark
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load the Word document from disk
document.LoadFromFile(@"D:\testp\test.docx");
//Find text you want to emphasize
TextSelection[] textSelections = document.FindAllString("Spire.Doc for .NET", false, true);
//Apply emphasis mark to the found text
foreach (TextSelection selection in textSelections)
{
selection.GetAsOneRange().CharacterFormat.EmphasisMark = Emphasis.Dot;
}
//Save the document to another Word file
string output = "ApllyEmphasisMark.docx";
document.SaveToFile(output, FileFormat.Docx);
}
}
}
以上便是如何在 Word 中应用强调标记(C#/VB.NET),如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线