Aspose.Words for .NET查找和替换教程——如何查找和突出显示文本

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

接下来我们将进入“查找和替换”的介绍,在Aspose.Words中如何查找和突出显示文本

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

Aspose优惠进行时——购买Aspose系列产品可享限时优惠!更多活动详情可咨询在线客服哦~


世界您好

除了格式化外,文本中间的书签还将其分成更多的行。)  上面的示例在Aspose.Words中使用以下对象表示:

  • Run (Run.Text = “Hello”, Font.Italic = true)

  • Run (Run.Text = “World”, Font.Bold = true)

  • Run (Run.Text = “!”)

该示例代码将打开一个文档,并找到文本“您的文档”的任何实例。设置了替换处理程序,以处理要应用于找到的每个结果匹配项的逻辑。在这种情况下,结果运行将围绕文本进行拆分,结果运行将突出显示。下面的示例查找并突出显示Word文档中特定单词或短语的所有实例。

//文档目录的路径string dataDir = RunExamples.GetDataDir_FindAndReplace();string fileName = "TestFile.doc";Document doc = new Document(dataDir + fileName);FindReplaceOptions options = new FindReplaceOptions();options.ReplacingCallback = new ReplaceEvaluatorFindAndHighlight();options.Direction = FindReplaceDirection.Backward;//我们希望突出显示“您的文档”短语。Regex regex = new Regex("your document", RegexOptions.IgnoreCase);doc.Range.Replace(regex, "", options);dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);//保存输出文档。doc.Save(dataDir);
private class ReplaceEvaluatorFindAndHighlight : IReplacingCallback    {    /// <summary>    /// Aspose.Words为每个匹配项查找并替换引擎调用此方法。    ///即使跨多个运行,此方法也会突出显示匹配字符串。/// </summary>    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)    {    // 这是一个Run节点,其中包含开始或完全匹配。  Node currentNode = e.MatchNode;    // 第一次(可能是唯一一次)运行可以包含比赛前的文字,// 在这种情况下,有必要拆分运行。  if (e.MatchOffset > 0)    currentNode = SplitRun((Run)currentNode, e.MatchOffset);    // 此数组用于存储匹配的所有节点以进一步突出显示。  ArrayList runs = new ArrayList();    // 查找包含匹配字符串部分的所有运行。    int remainingLength = e.Match.Value.Length;    while (    (remainingLength > 0) &&    (currentNode != null) &&    (currentNode.GetText().Length <= remainingLength))    {    runs.Add(currentNode);    remainingLength = remainingLength - currentNode.GetText().Length;    //选择下一个“运行”节点。//必须循环,因为可能还有其他节点,例如BookmarkStart等。 do    {    currentNode = currentNode.NextSibling;    }    while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));    }    //如果剩余文本,则拆分包含该匹配项的最后一次运行。    if ((currentNode != null) && (remainingLength > 0))    {    SplitRun((Run)currentNode, remainingLength);    runs.Add(currentNode);    }    //现在突出显示序列中的所有运行。foreach (Run run in runs)    run.Font.HighlightColor = Color.Yellow;    //向替换引擎发出信 ,表示什么都不做,因为我们已经完成了所有想要做的事情。return ReplaceAction.Skip;    }    }
/// <summary>    /// 将指定运行的文本分成两个运行。    /// 在指定的运行之后插入新的运行。  /// </summary>    private static Run SplitRun(Run run, int position)    {    Run afterRun = (Run)run.Clone(true);    afterRun.Text = run.Text.Substring(position);    run.Text = run.Text.Substring(0, position);    run.ParentNode.InsertAfter(afterRun, run);    return afterRun;    }

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


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

标签:

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

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

相关推荐

发表回复

登录后才能评论