Replacing
内容
[
隐藏
]IReplacingCallback.Replacing method
用户定义的方法,在替换操作期间为替换之前找到的每个匹配项调用。
public ReplaceAction Replacing(ReplacingArgs args)
返回值
AReplaceAction
指定当前匹配要采取的操作的值。
例子
演示如何将所有出现的正则表达式模式替换为另一个字符串,同时跟踪所有此类替换。
public void ReplaceWithCallback()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Our new location in New York City is opening tomorrow. " +
"Hope to see all our NYC-based customers at the opening!");
// 我们可以使用“FindReplaceOptions”对象来修改查找和替换过程。
FindReplaceOptions options = new FindReplaceOptions();
// 设置一个回调来跟踪“Replace”方法将进行的任何替换。
TextFindAndReplacementLogger logger = new TextFindAndReplacementLogger();
options.ReplacingCallback = logger;
doc.Range.Replace(new Regex("New York City|NYC"), "Washington", options);
Assert.AreEqual("Our new location in (Old value:\"New York City\") Washington is opening tomorrow. " +
"Hope to see all our (Old value:\"NYC\") Washington-based customers at the opening!", doc.GetText().Trim());
Assert.AreEqual("\"New York City\" converted to \"Washington\" 20 characters into a Run node.\r\n" +
"\"NYC\" converted to \"Washington\" 42 characters into a Run node.", logger.GetLog().Trim());
}
/// <summary>
/// 维护查找和替换操作完成的每个文本替换的日志
/// 并记录原始匹配文本的值。
/// </summary>
private class TextFindAndReplacementLogger : IReplacingCallback
{
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
{
mLog.AppendLine($"\"{args.Match.Value}\" converted to \"{args.Replacement}\" " +
$"{args.MatchOffset} characters into a {args.MatchNode.NodeType} node.");
args.Replacement = $"(Old value:\"{args.Match.Value}\") {args.Replacement}";
return ReplaceAction.Replace;
}
public string GetLog()
{
return mLog.ToString();
}
private readonly StringBuilder mLog = new StringBuilder();
}
演示如何在查找和替换操作中插入整个文档的内容作为匹配项的替换。
public void InsertDocumentAtReplace()
{
Document mainDoc = new Document(MyDir + "Document insertion destination.docx");
// 我们可以使用“FindReplaceOptions”对象来修改查找和替换过程。
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new InsertDocumentAtReplaceHandler();
mainDoc.Range.Replace(new Regex("\\[MY_DOCUMENT\\]"), "", options);
mainDoc.Save(ArtifactsDir + "InsertDocument.InsertDocumentAtReplace.docx");
}
private class InsertDocumentAtReplaceHandler : IReplacingCallback
{
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
{
Document subDoc = new Document(MyDir + "Document.docx");
// 在包含匹配文本的段落之后插入文档。
Paragraph para = (Paragraph)args.MatchNode.ParentNode;
InsertDocument(para, subDoc);
// 删除具有匹配文本的段落。
para.Remove();
return ReplaceAction.Skip;
}
}
/// <summary>
/// 在段落或表格之后插入另一个文档的所有节点。
/// </summary>
private static void InsertDocument(Node insertionDestination, Document docToInsert)
{
if (insertionDestination.NodeType == NodeType.Paragraph || insertionDestination.NodeType == NodeType.Table)
{
CompositeNode dstStory = insertionDestination.ParentNode;
NodeImporter importer =
new NodeImporter(docToInsert, insertionDestination.Document, ImportFormatMode.KeepSourceFormatting);
foreach (Section srcSection in docToInsert.Sections.OfType<Section>())
foreach (Node srcNode in srcSection.Body)
{
// 如果该节点是节中最后一个空段落,则跳过该节点。
if (srcNode.NodeType == NodeType.Paragraph)
{
Paragraph para = (Paragraph)srcNode;
if (para.IsEndOfSection && !para.HasChildNodes)
continue;
}
Node newNode = importer.ImportNode(srcNode, true);
dstStory.InsertAfter(newNode, insertionDestination);
insertionDestination = newNode;
}
}
else
{
throw new ArgumentException("The destination node must be either a paragraph or table.");
}
}
也可以看看
- enum ReplaceAction
- class ReplacingArgs
- interface IReplacingCallback
- 命名空间 Aspose.Words.Replacing
- 部件 Aspose.Words