Aspose.Words for .NET查找和替换教程——使用元字符查找和替换文本

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

接下来我们将进入“查找和替换”的介绍,在Aspose.Words中使用元字符查找和替换文本

>>Aspose.Words for .NET更新至最新版v19.11,欢迎下载体验

Aspose优惠进行时——购买Aspose系列产品有机会获得iPhone 11等惊喜豪礼!更多活动详情可咨询在线客服哦~


如何在替换期间保留元字符

如果要在查找和替换过程中保留以“&”开头的元字符,则可以使用FindReplaceOptions.PreserveMetaCharacters属性。下面的代码示例演示如何查找文本并将其替换为具有元字符的HTML。

//文档目录的路径。string dataDir = RunExamples.GetDataDir_FindAndReplace();string html = @"“Some Text”";//初始化文档。Document doc = new Document();//使用文档构建器向文档添加内容。DocumentBuilder builder = new DocumentBuilder(doc);builder.Write("{PLACEHOLDER}");var findReplaceOptions = new FindReplaceOptions{    ReplacingCallback = new FindAndInsertHtml(),    PreserveMetaCharacters = true};doc.Range.Replace("{PLACEHOLDER}", html, findReplaceOptions);dataDir = dataDir + "ReplaceHtmlTextWithMetaCharacters_out.doc";doc.Save(dataDir);
public sealed class FindAndInsertHtml : IReplacingCallback{    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)    {        // 这是一个Run节点,其中包含开始或完全匹配。        Node currentNode = e.MatchNode;        //创建Document Buidler并插入MergeField        DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);        builder.MoveTo(currentNode);        builder.InsertHtml(e.Replacement);        currentNode.Remove();        //向替换引擎发出信 ,表示什么都不做,因为我们已经完成了所有想要做的事情。        return ReplaceAction.Skip;    }}

使用元字符替换文本

Range.Replace方法支持搜索模式和替换字符串中的中断。您需要使用特殊的元字符来指定分隔符:&p代表段落分隔符,&b代表分节符,&m代表分页符,&l代表手动换行符。下面的代码示例显示了如何替换包含段落和分页符的文本。

// 初始化文档。Document doc = new Document();// 使用文档构建器向文档添加内容。DocumentBuilder builder = new DocumentBuilder(doc);builder.Writeln("This is Line 1");builder.Writeln("This is Line 2");var findReplaceOptions = new FindReplaceOptions();doc.Range.Replace("This is Line 1&pThis is Line 2", "This is replaced line", findReplaceOptions);builder.MoveToDocumentEnd();builder.Write("This is Line 1");builder.InsertBreak(BreakType.PageBreak);builder.Writeln("This is Line 2");doc.Range.Replace("This is Line 1&mThis is Line 2", "Page break is replaced with new text.", findReplaceOptions);dataDir = dataDir + "MetaCharactersInSearchPattern_out.docx";doc.Save(dataDir);

下面的代码示例显示如何用新的段落分隔符替换某些段落分隔符以及如何用分节符替换自定义文本标记。

Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);builder.Font.Name = "Arial";builder.Writeln("First section");builder.Writeln("  1st paragraph");builder.Writeln("  2nd paragraph");builder.Writeln("{insert-section}");builder.Writeln("Second section");builder.Writeln("  1st paragraph");FindReplaceOptions options = new FindReplaceOptions();options.ApplyParagraphFormat.Alignment = ParagraphAlignment.Center;// 将“ section”一词后的每个段落加倍,添加下划线并使其居中。int count = doc.Range.Replace("section&p", "section&p----------------------&p", options);// 插入分节符而不是自定义文本标签。count = doc.Range.Replace("{insert-section}", "&b", options);dataDir = dataDir + "ReplaceTextContaingMetaCharacters_out.docx";doc.Save(dataDir);

还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群,我们很高兴为您提供查询和咨询


如果您对Aspose有任何需求和疑难,记得扫描下方二维码告诉我们哦~

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年10月9日
下一篇 2019年10月9日

相关推荐

发表回复

登录后才能评论