Aspose.Words使用教程之在文檔中找到並替換文本
Aspose.Words是一款先進的文檔處理控件,在不使用Microsoft Words的情況下,它可以使用戶在各個應用程序中執行各種文檔處理任務,其中包括文檔的生成、修改、渲染、打印,文檔格式轉換和郵件合併等文檔處理。此外,Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocument, PDF, XPS, EPUB和其他格式。
使用範圍:在前的範圍內替換查找或替換特定的字符串,因爲它會返回替換的數量,所以它是在沒有替換的條件下搜索字符串是非常有用的。如果一個捕獲或替換包含一個或多個特殊字符:段落,細胞破裂,部分斷裂,現場開始,字段分隔符、字段,內聯圖片,繪畫對象,腳註的字符串時,會出現異常時。
在一定的範圍內,替代方法提供了幾個過載。以下是他們提供的可能性:
您可以指定一個字符串被替換,一旦被替換,所有這個字符串都將被替換,替換是否區分大小寫,或者只有獨立的單詞纔會受到影響。注意一個詞被定義爲僅由字母數字字符組成。如果只執行替換匹配的整個單詞,輸入字符串包含了符號,那麼這個單詞不會被搜索到。
您可以通過一個正則表達式模式用於查找匹配和一個字符串,然後替換它們。這種過載替換通過正則表達式捕捉到整個匹配。
你可以通過正則表達式模式和一個對象實現[` IReplacingCallback `]接口。這提出了一個用戶定義的方法,它在每個步驟中評估替換,您也可以指示更換是應該向前還是向後的方向。建議如果在更換過程中要刪除節點那麼更換應該向後執行,以避免在更換過程中刪除節點有任何可能出現的潛在問題。一個實現回調接口的類將定義一個 [IReplacingCallback.Replacing]方法,它接受提供定製的替換數據操作[{ { ReplacingArgs } }]對象。這個方法應該返回一個[{ { ReplaceAction } }]枚舉值,指定當前匹配更換操作期間發生了什麼——它是否應該更換,跳過,或整個替換操作應該終止。
下面的例子展示如何使用前面提到的過載。樣例類提供的使用了Range.Replace 方法:
示例1 將所有出現的“sad”替換爲“bad”。
示例2 將所有出現的“sad”或者“mad”替換成“bad”。
示例3 使用替換評估方法來連接出現的單詞“sad”或“bad”,每出現一次計數值相應增加。
Example 1: 用一個詞換另一個詞
將所有出現的"sad"替換爲"bad"。
C#
Document doc = new Document(MyDir + @"in.docx");
doc.Range.Replace("sad", "bad", false, true);
Visual Basic
Dim doc As New Document(MyDir & "Document.doc")
doc.Range.Replace("sad", "bad", False, True)
Example 2: 用一個詞替換兩個相近的詞
使用“bad”替換所有“sad”和“mad”。
C#
Document doc = new Document(MyDir + "Document.doc");
doc.Range.Replace(new Regex("[s|m]ad"), "bad");
Visual Basic
Dim doc As New Document(MyDir & "Document.doc")
doc.Range.Replace(New Regex("[s|m]ad"), "bad")
Example 3:使用一個自定義計數器
如何替換爲一個自定義計數器
C#
public void ReplaceWithEvaluator()
{
Document doc = new Document(MyDir + "Range.ReplaceWithEvaluator.doc"); doc.Range.Replace(new Regex("[s|m]ad"), new MyReplaceEvaluator(), true); doc.Save(MyDir + "Range.ReplaceWithEvaluator Out.doc"); } private class MyReplaceEvaluator : IReplacingCallback { /// <summary> /// This is called during a replace operation each time a match is found. /// This method appends a number to the match string and returns it as a replacement string. /// </summary> ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e) { e.Replacement = e.Match.ToString() + mMatchNumber.ToString(); mMatchNumber++; return ReplaceAction.Replace; } private int mMatchNumber; } Public Sub ReplaceWithEvaluator() Dim doc As New Document(MyDir & "Range.ReplaceWithEvaluator.doc") doc.Range.Replace(New Regex("[s|m]ad"), New MyReplaceEvaluator(), True) doc.Save(MyDir & "Range.ReplaceWithEvaluator Out.doc") End Sub Private Class MyReplaceEvaluator Implements IReplacingCallback ''' <summary> ''' This is called during a replace operation each time a match is found. ''' This method appends a number to the match string and returns it as a replacement string. Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing e.Replacement = e.Match.ToString() & mMatchNumber.ToString() mMatchNumber += 1 Return ReplaceAction.Replace End Function Private mMatchNumber As Integer End Class