IReplacingCallback
内容
[
隐藏
]IReplacingCallback interface
如果您想在查找和替换操作期间调用自己的自定义方法,请实现此接口。
public interface IReplacingCallback
方法
姓名 | 描述 |
---|---|
Replacing(ReplacingArgs) | 用户定义的方法,在替换操作期间为替换之前找到的每个匹配项调用。 |
例子
演示如何将所有出现的正则表达式模式替换为另一个字符串,同时跟踪所有此类替换。
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 Order(bool differentFirstPageHeaderFooter)
{
Document doc = new Document(MyDir + "Header and footer types.docx");
Section firstPageSection = doc.FirstSection;
ReplaceLog logger = new ReplaceLog();
FindReplaceOptions options = new FindReplaceOptions { ReplacingCallback = logger };
// 第一页使用不同的页眉/页脚会影响搜索顺序。
firstPageSection.PageSetup.DifferentFirstPageHeaderFooter = differentFirstPageHeaderFooter;
doc.Range.Replace(new Regex("(header|footer)"), "", options);
#if NET48 || NET5_0_OR_GREATER || JAVA
if (differentFirstPageHeaderFooter)
Assert.AreEqual("First header\nFirst footer\nSecond header\nSecond footer\nThird header\nThird footer\n",
logger.Text.Replace("\r", ""));
else
Assert.AreEqual("Third header\nFirst header\nThird footer\nFirst footer\nSecond header\nSecond footer\n",
logger.Text.Replace("\r", ""));
#elif __MOBILE__
if (differentFirstPageHeaderFooter)
Assert.AreEqual("First header\nFirst footer\nSecond header\nSecond footer\nThird header\nThird footer\n", logger.Text);
else
Assert.AreEqual("Third header\nFirst header\nThird footer\nFirst footer\nSecond header\nSecond footer\n", logger.Text);
#endif
}
/// <summary>
/// 在查找和替换操作期间,记录具有操作“查找”文本的每个节点的内容,
/// 处于替换发生之前的状态。
/// 这将显示文本替换操作遍历节点的顺序。
/// </summary>
private class ReplaceLog : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs args)
{
mTextBuilder.AppendLine(args.MatchNode.GetText());
return ReplaceAction.Skip;
}
internal string Text => mTextBuilder.ToString();
private readonly StringBuilder mTextBuilder = 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.");
}
}