彩票走势图

使用 TX Text Control 和 C# 基于 Word 的文档比较和跟踪更改

翻译|行业资讯|编辑:胡涛|2024-05-20 11:29:46.270|阅读 6 次

概述:在文档处理应用程序中比较文档的策略有很多。最常见的方法之一是逐字比较文档的文本。这是一种简单有效的文档比较方法,但它确实有一些局限性。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

TX Text Control 是一款功能类似于 MS Word 的文字处理控件,包括文档创建、编辑、打印、邮件合并、格式转换、拆分合并、导入导出、批量生成等功能。广泛应用于企业文档管理,网站内容发布,电子病历中病案模板创建、病历书写、修改历史、连续打印、病案归档等功能的实现。

在文档处理应用程序中比较文档的策略有很多。最常见的方法之一是逐字比较文档的文本。这是一种简单有效的文档比较方法,但它确实有一些局限性。

TX Text Control 最新下载

逐字比较

本质上,该比较算法按给定顺序比较所有段落。在段落的基础上,将按照分隔符提取所有句子。最后,将原始文档中这些句子中的单词与给定的修订文档进行比较。

结果在原始文档中标记为跟踪更改。跟踪更改在原始文档中突出显示,用户可以看到对文档所做的更改。

C# 中的文档与 TX 文本控件的比较

执行

该示例实现了该类DocumentComparison,该类接受两个TXText控件。 其构造函数中的文本控件实例。您可以轻松地重写此类以使用非 UI TXText控件。服务器文本控件实例。

DocumentComparison dc = new DocumentComparison(textControl1, textControl2);

构造函数比较两个文档。它循环遍历原始文档中的所有段落,并将文本与修订后的文档进行比较。如果发现差异,文本将被标记为跟踪更改。

提取句子

该ExtractSentences方法从当前段落中获取一个字符串,并通过在典型的分隔符处拆分它来返回句子列表。

public static List<string> ExtractSentences(string input)
{
List<string> sentences = new List<string>();

// Use regular expression to split the input string into sentences but keep white spaces
string pattern = @"([.!?])";

// split the input string into sentences with the delimiters
string[] splitSentences = Regex.Split(input, pattern);

// Trim each sentence and remove empty strings
foreach (string sentence in splitSentences)
{
sentences.Add(sentence);
}

return sentences;
}

比较句子

CompareSentences 方法创建单个单词并比较每个给定句子中单词的位置。它返回一个元组列表,每个元组包含三个元素:单词 from sentence1、单词开头的字符索引以及对应的单词 from sentence2。最后,它返回两个句子之间的差异列表。

private static List<(string word, int charIndex, string replacedWord)> CompareSentences(string sentence1, string sentence2)
{
string[] words1 = sentence1.Split(' ');
string[] words2 = sentence2.Split(' ');

List<(string word, int charIndex, string replacedWord)> differences =
new List<(string word, int charIndex, string replacedWord)>();

// Track the character index
int charIndex = 0;

// Get the maximum length of the two sentences
int maxLength = Math.Max(words1.Length, words2.Length);

// Compare each word in the sentences
for (int i = 0; i < maxLength; i++)
{
// Check if the current word exists in both sentences
if (i < words1.Length && i < words2.Length)
{
// If the words are different, add the word, character index, and replaced word to the list
if (words1[i] != words2[i])
{
differences.Add((words1[i], charIndex, words2[i]));
}
}
// If one of the sentences is shorter, add the extra word to the list
else if (i < words1.Length)
{
differences.Add((words1[i], charIndex, ""));
}
else
{
differences.Add((words2[i], charIndex, ""));
}

// Update the character index for the next word
if (i < words1.Length)
charIndex += words1[i].Length + 1; // Add 1 for the space
}

return differences;
}

比较文件

DocumentComparison 类的构造函数使用上述方法来查找给定 TextControl 实例之间的差异。差异被标记为原始文档中的跟踪更改。

public DocumentComparison(TXTextControl.TextControl originalDocument, TextControl revisedDocument)
{
// Initialize document references
m_originalDocument = originalDocument;
m_revisedDocument = revisedDocument;

// Enable track changes in the original document
originalDocument.IsTrackChangesEnabled = true;

// Compare paragraphs between the original and revised documents
for (int p = 1; p <= m_originalDocument.Paragraphs.Count; p++)
{
var offsetSentences = 0;

// Retrieve the original and revised paragraphs
Paragraph originalParagraph = m_originalDocument.Paragraphs[p];

if (p > m_revisedDocument.Paragraphs.Count)
break; // Break if the revised document has fewer paragraphs than the original document

Paragraph revisedParagraph = m_revisedDocument.Paragraphs[p];

// Get the start position of the original paragraph
var startParagraph = originalParagraph.Start;
var uncheckedOffset = 0;

// Check if the text of the original and revised paragraphs differ
if (originalParagraph.Text != revisedParagraph.Text)
{
// Extract sentences from the original and revised paragraphs
var originalSentences = ExtractSentences(originalParagraph.Text);
var revisedSentences = ExtractSentences(revisedParagraph.Text);

// Compare sentences and replace words in the original document
for (int i = 0; i < originalSentences.Count; i++)
{
// Trim sentences and calculate offset
var originalTrimOffset = originalSentences[i].Length - originalSentences[i].Trim().Length;
var originalSentence = originalSentences[i].Trim();
var revisedSentence = revisedSentences[i].Trim();

// Track changes offset initialization
int trackedChangeOffset = 0;

var differences = CompareSentences(originalSentence, revisedSentence);

// Check if there are any differences
if (differences.Count == 0)
uncheckedOffset = originalSentences[i].Length - 1;

// Apply differences to the original document
foreach (var difference in differences)
{
m_originalDocument.Selection.Start = trackedChangeOffset + startParagraph + offsetSentences +
difference.charIndex + originalTrimOffset + uncheckedOffset - 1;

m_originalDocument.Selection.Length = difference.word.Length;
m_originalDocument.Selection.Text = difference.replacedWord;

trackedChangeOffset += difference.replacedWord.Length;
}

// Update offset for next sentence
offsetSentences += originalSentences[i].Length + trackedChangeOffset;
}
}

}
}

结论

逐字比较文档是文档比较的常用方法。此示例演示如何使用 TX Text Control 实现简单的逐字比较算法。该示例比较两个文档并将差异标记为跟踪原始文档中的更改。


欢迎下载|体验更多TX Text Control产品

获取更多产品信息请咨询 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP