忽略插入修订内的文本

在本文中,我们将探索上面的 C# 源代码,以了解如何使用 Aspose.Words for .NET 库中的 Ignore Text Inside Insert Revisions 功能。当我们在操作文档时想要忽略插入修订中的文本时,此功能非常有用。

先决条件

  • C# 语言的基础知识。
  • 安装了 Aspose.Words 库的 .NET 开发环境。

第 1 步:创建新文档

在我们开始操作插入修订中的文本之前,我们需要使用 Aspose.Words for .NET 创建一个新文档。这可以通过实例化一个来完成Document目的:

Document doc = new Document();

第 2 步:插入带有修订跟踪的文本

一旦我们有了文档,我们就可以使用DocumentBuilder目的。例如,要插入带有修订跟踪的“已插入”文本,我们可以使用StartTrackRevisions, WritelnStopTrackRevisions方法:

DocumentBuilder builder = new DocumentBuilder(doc);
doc.StartTrackRevisions("author", DateTime.Now);
builder.Writeln("Inserted");
doc.StopTrackRevisions();

第 3 步:插入未经审阅的文本

除了带有修订跟踪的文本之外,我们还可以使用DocumentBuilder目的。例如,要插入文本“Text”而不进行修改,我们可以使用Write方法:

builder.Write("Text");

步骤 4:使用“忽略插入修订内的文本”功能

要在后续操作中忽略插入修订内的文本,我们可以使用FindReplaceOptions对象并设置IgnoreInserted财产给true:

FindReplaceOptions options = new FindReplaceOptions { IgnoreInserted = true };

步骤 5:使用正则表达式进行搜索和替换

为了对文档文本执行搜索操作和替换,我们将使用正则表达式。在我们的示例中,我们将搜索所有出现的字母“e”并将其替换为星号“*“。我们将使用 .NETRegex为此类:

Regex regex = new Regex("e");
doc.Range.Replace(regex, "*", options);

第6步:查看修改后的文档输出

应用搜索和替换后,我们可以使用以下命令显示文档的更改内容GetText方法:

Console.WriteLine(doc.GetText());

步骤 7:更改选项以包括插入修订

如果我们想在输出结果中包含插入修订内的文本,我们可以更改选项以不忽略插入修订。为此我们将设置IgnoreInserted财产给false:

options.IgnoreInserted = false;

步骤 8:查看带有插入修订的修改文档

更改选项后,我们可以再次执行搜索并替换以获得包含插入修订内文本的结果:

doc.Range.Replace(regex, "*", options);
Console.WriteLine(doc.GetText());

使用 Aspose.Words for .NET 忽略插入修订内的文本的示例源代码

以下是完整的示例源代码,演示如何使用 Aspose.Words for .NET 忽略插入修订内的文本功能:

       
	Document doc = new Document();
	DocumentBuilder builder = new DocumentBuilder(doc);

	//插入带有跟踪修订的文本。
	doc.StartTrackRevisions("author", DateTime.Now);
	builder.Writeln("Inserted");
	doc.StopTrackRevisions();

	//插入未修改的文本。
	builder.Write("Text");

	FindReplaceOptions options = new FindReplaceOptions { IgnoreInserted = true };

	Regex regex = new Regex("e");
	doc.Range.Replace(regex, "*", options);
	
	Console.WriteLine(doc.GetText());

	options.IgnoreInserted = false;
	doc.Range.Replace(regex, "*", options);
	
	Console.WriteLine(doc.GetText());
   

结论

在本文中,我们探索了 C# 源代码,以了解如何在 Aspose.Words for .NET 中使用 Ignore Text Inside Insert Revisions 功能。我们按照分步指南创建文档,插入带有跟踪修订和未修订文本的文本,使用“忽略插入修订内的文本”功能,使用正则表达式执行搜索和替换操作,并显示修改后的文档。

常见问题解答

问:Aspose.Words for .NET 中的“忽略插入修订中的文本”功能是什么?

答:Aspose.Words for .NET 中的“忽略插入修订内的文本”功能允许您指定在某些操作(例如查找和替换文本)期间是否应忽略插入修订内的文本。启用此功能后,操作期间不会考虑插入修订内的文本。

问:如何使用 Aspose.Words for .NET 创建新文档?

答:要使用 Aspose.Words for .NET 创建新文档,您可以实例化一个Document目的。以下是创建新文档的 C# 代码示例:

Document doc = new Document();

问:如何在 Aspose.Words for .NET 中插入带有修订跟踪的文本?

答:一旦您有了文档,您就可以使用DocumentBuilder目的。例如,要插入带有修订跟踪的“已插入”文本,您可以使用StartTrackRevisions, Writeln, 和StopTrackRevisions方法:

DocumentBuilder builder = new DocumentBuilder(doc);
doc.StartTrackRevisions("author", DateTime.Now);
builder.Writeln("Inserted");
doc.StopTrackRevisions();

问:如何在 Aspose.Words for .NET 中插入未修改的文本?

答:除了带有修订跟踪的文本之外,您还可以使用DocumentBuilder目的。例如,要插入文本“Text”而不进行修改,您可以使用Write方法:

builder.Write("Text");

问:如何忽略 Aspose.Words for .NET 中插入修订中的文本?

答:要在后续操作中忽略插入修订内的文本,您可以使用FindReplaceOptions对象并设置IgnoreInserted财产给true:

FindReplaceOptions options = new FindReplaceOptions { IgnoreInserted = true };

问:如何在 Aspose.Words for .NET 中使用正则表达式执行搜索和替换?

答:要使用正则表达式对文档文本执行搜索和替换操作,可以使用.NETRegex班级。例如,要搜索所有出现的字母“e”并将其替换为星号“* “,您可以创建一个Regex对象并将其与Replace方法:

Regex regex = new Regex("e");
doc.Range.Replace(regex, "*", options);

问:如何在 Aspose.Words for .NET 中查看文档的修改输出?

A:应用搜索和替换操作后,您可以使用以下命令查看文档的更改内容:GetText方法:

Console.WriteLine(doc.GetText());

问:如何在 Aspose.Words for .NET 的输出结果中包含插入修订?

答:要在输出结果中包含插入修订内的文本,您可以更改选项以不忽略插入修订。为此,您可以设置IgnoreInserted的财产FindReplaceOptions反对false:

options.IgnoreInserted = false;

问:如何在 Aspose.Words for .NET 中显示带有插入修订的修改文档?

答:更改选项以包含插入修订后,您可以再次执行搜索并替换以获得包含插入修订内文本的结果:

doc.Range.Replace(regex, "*", options);
Console.WriteLine(doc.GetText());