翻譯|使用教程|編輯:胡濤|2022-07-19 13:53:37.130|閱讀 187 次
概述:本指南重點介紹使用 C#、VB.NET 在 Word 中查找和突出顯示文本的解決方案。請查看以下屏幕截圖,它顯示了您可以獲得的結果。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在包含大量內容的 Word 文檔中查找指定的文本、短語或句子有點麻煩。因此,讓用戶在Word文檔中快速查找文本的功能是導入且必要的。更重要的是,找到的文本會以背景顏色突出顯示,以便用戶更方便地確認位置。
Spire.Doc for .NET是一個專業的.NET Word 組件,使開發人員能夠在其 .NET 應用程序上查找并突出顯示 Word 中的文本。本指南重點介紹使用 C#、VB.NET 在 Word 中查找和突出顯示文本的解決方案。請查看以下屏幕截圖,它顯示了您可以獲得的結果。
下載并安裝 Spire.Doc for .NET。加載要查找的文檔并突出顯示文本。然后,調用document.FindAllString(string, bool caseSensitive, bool wholeWord)方法在文檔中查找“正在形成”的文本。接下來,將找到的字符串保存在TextSelection 數組中。使用 foreach 語句為 TextSelection 數組中的每個選擇設置 HighlightColor 屬性。代碼如下
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:\Work\Documents\WordDocuments\References.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:\Work\Documents\WordDocuments\References.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產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn