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{    ///     /// 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.    ///     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 SubPrivate Class MyReplaceEvaluator    Implements IReplacingCallback    '''     ''' 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 IntegerEnd Class