Word控件Spire.Doc 【文本】教程(3) ;在 C#、VB.NET 中的 Word 中查找和突出显示文本

本指南重点介绍使用 C#、VB.NET 在 Word 中查找和突出显示文本的解决方案。请查看以下屏幕截图,它显示了您可以获得的结果。

在包含大量内容的 Word 文档中查找指定的文本、短语或句子有点麻烦。因此,让用户在Word文档中快速查找文本的功能是导入且必要的。更重要的是,找到的文本会以背景颜色突出显示,以便用户更方便地确认位置。

Spire.Doc for .NET是一个专业的.NET Word 组件,使开发人员能够在其 .NET 应用程序上查找并突出显示 Word 中的文本。本指南重点介绍使用 C#、VB.NET 在 Word 中查找和突出显示文本的解决方案。请查看以下屏幕截图,它显示了您可以获得的结果。

查找并突出显示 Word 文本

下载并安装 Spire.Doc for .NET。加载要查找的文档并突出显示文本。然后,调用document.FindAllString(string, bool caseSensitive, bool wholeWord)方法在文档中查找“正在形成”的文本。接下来,将找到的字符串保存在TextSelection 数组中。使用 foreach 语句为 TextSelection 数组中的每个选择设置 HighlightColor 属性。代码如下

[C#]
using System.Drawing;<font></font>using Spire.Doc;<font></font>using Spire.Doc.Documents;<font></font><font></font>namespace WordImage<font></font>{<font></font>class ImageinWord<font></font>{<font></font>static void Main(string[] args)<font></font>{<font></font>//Create Document<font></font>Document document = new Document();<font></font>document.LoadFromFile(@"E:WorkDocumentsWordDocumentsReferences.docx");<font></font><font></font>TextSelection[] text = document.FindAllString("forming", false, true);<font></font>foreach (TextSelection seletion in text)<font></font>{<font></font>seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;<font></font>}<font></font><font></font>document.SaveToFile("FindHighlight.docx", FileFormat.Docx);<font></font>System.Diagnostics.Process.Start("FindHighlight.docx");<font></font>}<font></font>}<font></font>}<font></font>

[VB]

Imports System.Drawing<font></font>Imports Spire.Doc<font></font>Imports Spire.Doc.Documents<font></font><font></font>Namespace WordImage<font></font>Friend Class ImageinWord<font></font>Shared Sub Main(ByVal args() As String)<font></font>'Create Document<font></font>Dim document As New Document()<font></font>document.LoadFromFile("E:WorkDocumentsWordDocumentsReferences.docx")<font></font><font></font>Dim text() As TextSelection = document.FindAllString("forming", False, True)<font></font>For Each seletion As TextSelection In text<font></font>seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow<font></font>Next seletion<font></font><font></font>document.SaveToFile("FindHighlight.docx", FileFormat.Docx)<font></font>System.Diagnostics.Process.Start("FindHighlight.docx")<font></font>End Sub<font></font>End Class<font></font>End Namespace<font></font>

Spire.Doc,一个独立的 Word 组件,它使用户能够快速执行各种 Word 文档处理任务,例如在 WPF、.NET 和 Silverlight 中生成、读取、写入和修改 Word 文档,而无需 MS Word 自动化.


欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

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

上一篇 2022年6月16日
下一篇 2022年6月16日

相关推荐

发表回复

登录后才能评论