轉帖|使用教程|編輯:黃竹雯|2019-01-17 10:35:31.000|閱讀 2883 次
概述:Spire.Doc系列教程之使用C#來實現word查找,替換和高亮顯示功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc為開發者提供了查找和替換功能的方法,我們可以通過document.FindString()方法查找文檔中某一個特定詞匯并對它進行高亮替換, 也可以通過document.FindAllString()方法查找文本中所有地該詞匯對將找到的詞匯使用Document.Replace()方法進行替換更改。本文將詳細介紹如何使用C#來實現word查找,替換和高亮顯示功能。
//新建一個word文檔對象并加載sample文檔 Document document = new Document(); document.LoadFromFile("Test.docx", FileFormat.Docx2010); //查找一個特定字符串 ”Spire.Doc” TextSelection selection = document.FindString("Spire.Doc", false, true); TextRange range = selection.GetAsOneRange(); //替換字符串 range.Text = "Replaced Text"; //設置高亮顏色 range.CharacterFormat.HighlightColor = Color.Yellow; //查找文檔中所有字符串 ”Microsoft” TextSelection[] text = document.FindAllString("Microsoft", false, true); //設置高亮顏色 foreach (TextSelection seletion in text) { seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Green; } //使用 ”MS” 替換所有 ”Microsoft” document.Replace("Microsoft", "MS", false, true); //保存文檔 document.SaveToFile("Result.docx", FileFormat.Docx2010);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn