مقایسه اسناد

مقایسه اسناد فرآیندی است که تغییرات بین دو سند را شناسایی می‌کند و تغییرات را به‌عنوان تجدیدنظر در بر می‌گیرد. این فرآیند هر دو سند را با هم مقایسه می‌کند، از جمله نسخه‌های یک سند خاص، سپس تغییرات بین هر دو سند به عنوان تجدید نظر در سند اول نشان داده می‌شود.

روش مقایسه با مقایسه کلمات در سطح کاراکتر یا در سطح کلمه به دست می آید. اگر یک کلمه شامل تغییر حداقل یک کاراکتر باشد، در نتیجه، تفاوت به عنوان تغییر کل کلمه نمایش داده می شود، نه یک کاراکتر. این فرآیند مقایسه یک کار معمول در صنایع حقوقی و مالی است.

به جای جستجوی دستی تفاوت بین اسناد یا بین نسخه‌های مختلف آنها، می‌توانید از Aspose.Words برای مقایسه اسناد و دریافت تغییرات محتوا در قالب‌بندی، سرصفحه/پانویس، جداول و موارد دیگر استفاده کنید.

این مقاله نحوه مقایسه اسناد و نحوه تعیین ویژگی های مقایسه پیشرفته را توضیح می دهد.

محدودیت ها و فرمت های فایل پشتیبانی شده

مقایسه اسناد یک ویژگی بسیار پیچیده است. بخش های مختلفی از ترکیب محتوا وجود دارد که برای تشخیص همه تفاوت ها باید تجزیه و تحلیل شوند. دلیل این پیچیدگی به این دلیل است که هدف Aspose.Words بدست آوردن نتایج مقایسه مشابه با الگوریتم مقایسه Microsoft Word است.

محدودیت کلی برای دو سند در حال مقایسه این است که آنها نباید قبل از فراخوانی روش مقایسه تجدید نظر داشته باشند زیرا این محدودیت در Microsoft Word وجود دارد.

مقایسه دو سند

وقتی اسناد را با هم مقایسه می کنید، تفاوت های سند دوم از اولی به عنوان تجدید نظر در مورد اول نشان داده می شود. هنگامی که یک سند را تغییر می دهید، هر ویرایش پس از اجرای روش مقایسه، بازبینی خاص خود را خواهد داشت.

Aspose.Words به شما امکان می‌دهد تفاوت‌های اسناد را با استفاده از روش Compare شناسایی کنید - این شبیه به ویژگی مقایسه اسناد Microsoft Word است. این به شما امکان می دهد اسناد یا نسخه های سند را بررسی کنید تا تفاوت ها و تغییرات را پیدا کنید، از جمله اصلاحات قالب بندی مانند تغییر فونت، تغییر فاصله، افزودن کلمات و پاراگراف ها.

در نتیجه مقایسه می توان اسناد را برابر یا غیر مساوی تشخیص داد. اصطلاح اسناد “برابر” به این معنی است که روش مقایسه قادر به نمایش تغییرات به عنوان تجدید نظر نیست. این بدان معنی است که هر دو متن سند و قالب بندی متن یکسان هستند. اما می تواند تفاوت های دیگری بین اسناد وجود داشته باشد. به عنوان مثال، Microsoft Word فقط از ویرایش قالب برای سبک ها پشتیبانی می کند و شما نمی توانید درج/حذف سبک را نشان دهید. بنابراین اسناد می‌توانند مجموعه‌ای از سبک‌های متفاوت داشته باشند و روش Compare هنوز هیچ تجدیدنظری ایجاد نمی‌کند.

مثال کد زیر نشان می دهد که چگونه می توان بررسی کرد که آیا دو سند برابر هستند یا نه:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document docA = new Document(dataDir + "TestFile.doc");
Document docB = new Document(dataDir + "TestFile - Copy.doc");
// DocA now contains changes as revisions.
docA.Compare(docB, "user", DateTime.Now);
if (docA.Revisions.Count == 0)
Console.WriteLine("Documents are equal");
else
Console.WriteLine("Documents are not equal");

مثال کد زیر نحوه اعمال روش Compare را به دو سند نشان می دهد:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The source document doc1.
Document doc1 = new Document();
DocumentBuilder builder = new DocumentBuilder(doc1);
builder.Writeln("This is the original document.");
// The target document doc2.
Document doc2 = new Document();
builder = new DocumentBuilder(doc2);
builder.Writeln("This is the edited document.");
// If either document has a revision, an exception will be thrown.
if (doc1.Revisions.Count == 0 && doc2.Revisions.Count == 0)
doc1.Compare(doc2, "authorName", DateTime.Now);
// If doc1 and doc2 are different, doc1 now has some revisions after the comparison, which can now be viewed and processed.
Assert.AreEqual(2, doc1.Revisions.Count);
foreach (Revision r in doc1.Revisions)
{
Console.WriteLine($"Revision type: {r.RevisionType}, on a node of type \"{r.ParentNode.NodeType}\"");
Console.WriteLine($"\tChanged text: \"{r.ParentNode.GetText()}\"");
}
// All the revisions in doc1 are differences between doc1 and doc2, so accepting them on doc1 transforms doc1 into doc2.
doc1.Revisions.AcceptAll();
// doc1, when saved, now resembles doc2.
doc1.Save(dataDir + "Document.Compare.docx");
doc1 = new Document(dataDir + "Document.Compare.docx");
Assert.AreEqual(0, doc1.Revisions.Count);
Assert.AreEqual(doc2.GetText().Trim(), doc1.GetText().Trim());

گزینه های مقایسه پیشرفته {#specify-advanced-comparing-properties} را مشخص کنید

بسیاری از ویژگی های مختلف کلاس CompareOptions وجود دارد که می توانید در هنگام مقایسه اسناد از آنها استفاده کنید.

به عنوان مثال، Aspose.Words به شما امکان می دهد تغییرات ایجاد شده در طول عملیات مقایسه برای انواع خاصی از اشیاء در سند اصلی را نادیده بگیرید. می‌توانید ویژگی مناسب برای نوع شی مانند IgnoreHeadersAndFooters، IgnoreFormatting، IgnoreComments و موارد دیگر را با تنظیم آنها بر روی “true” انتخاب کنید.

علاوه بر این، Aspose.Words ویژگی Granularity را ارائه می دهد که با آن می توانید تعیین کنید که آیا تغییرات را بر اساس کاراکتر یا کلمه ردیابی کنید.

یکی دیگر از ویژگی های رایج، انتخاب سندی است که تغییرات مقایسه را نشان دهد. به عنوان مثال، “جعبه گفتگوی مقایسه اسناد” در Microsoft Word دارای گزینه “نمایش تغییرات در” است - این همچنین بر نتایج مقایسه تأثیر می گذارد. Aspose.Words ویژگی Target را فراهم می کند که به این منظور خدمت می کند.

مثال کد زیر نحوه تنظیم ویژگی های مقایسه پیشرفته را نشان می دهد:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Create the original document.
Document docOriginal = new Document();
DocumentBuilder builder = new DocumentBuilder(docOriginal);
// Insert paragraph text with an endnote.
builder.Writeln("Hello world! This is the first paragraph.");
builder.InsertFootnote(FootnoteType.Endnote, "Original endnote text.");
// Insert a table.
builder.StartTable();
builder.InsertCell();
builder.Write("Original cell 1 text");
builder.InsertCell();
builder.Write("Original cell 2 text");
builder.EndTable();
// Insert a textbox.
Shape textBox = builder.InsertShape(ShapeType.TextBox, 150, 20);
builder.MoveTo(textBox.FirstParagraph);
builder.Write("Original textbox contents");
// Insert a DATE field.
builder.MoveTo(docOriginal.FirstSection.Body.AppendParagraph(""));
builder.InsertField(" DATE ");
// Insert a comment.
Comment newComment = new Comment(docOriginal, "John Doe", "J.D.", DateTime.Now);
newComment.SetText("Original comment.");
builder.CurrentParagraph.AppendChild(newComment);
// Insert a header.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Writeln("Original header contents.");
// Create a clone of our document, which we will edit and later compare to the original.
Document docEdited = (Document)docOriginal.Clone(true);
Paragraph firstParagraph = docEdited.FirstSection.Body.FirstParagraph;
// Change the formatting of the first paragraph, change casing of original characters and add text.
firstParagraph.Runs[0].Text = "hello world! this is the first paragraph, after editing.";
firstParagraph.ParagraphFormat.Style = docEdited.Styles[StyleIdentifier.Heading1];
// Edit the footnote.
Footnote footnote = (Footnote)docEdited.GetChild(NodeType.Footnote, 0, true);
footnote.FirstParagraph.Runs[1].Text = "Edited endnote text.";
// Edit the table.
Table table = (Table)docEdited.GetChild(NodeType.Table, 0, true);
table.FirstRow.Cells[1].FirstParagraph.Runs[0].Text = "Edited Cell 2 contents";
// Edit the textbox.
textBox = (Shape)docEdited.GetChild(NodeType.Shape, 0, true);
textBox.FirstParagraph.Runs[0].Text = "Edited textbox contents";
// Edit the DATE field.
FieldDate fieldDate = (FieldDate)docEdited.Range.Fields[0];
fieldDate.UseLunarCalendar = true;
// Edit the comment.
Comment comment = (Comment)docEdited.GetChild(NodeType.Comment, 0, true);
comment.FirstParagraph.Runs[0].Text = "Edited comment.";
// Edit the header.
docEdited.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary].FirstParagraph.Runs[0].Text = "Edited header contents.";
// Apply different comparing options.
CompareOptions compareOptions = new CompareOptions();
compareOptions.IgnoreFormatting = false;
compareOptions.IgnoreCaseChanges = false;
compareOptions.IgnoreComments = false;
compareOptions.IgnoreTables = false;
compareOptions.IgnoreFields = false;
compareOptions.IgnoreFootnotes = false;
compareOptions.IgnoreTextboxes = false;
compareOptions.IgnoreHeadersAndFooters = false;
compareOptions.Target = ComparisonTargetType.New;
// compare both documents.
docOriginal.Compare(docEdited, "John Doe", DateTime.Now, compareOptions);
docOriginal.Save(dataDir + "Document.CompareOptions.docx");
docOriginal = new Document(dataDir + "Document.CompareOptions.docx");
// If you set compareOptions to ignore certain types of changes,
// then revisions done on those types of nodes will not appear in the output document.
// You can tell what kind of node a revision was done on by looking at the NodeType of the revision's parent nodes.
Assert.AreNotEqual(compareOptions.IgnoreFormatting, docOriginal.Revisions.Any(rev => rev.RevisionType == RevisionType.FormatChange));
Assert.AreNotEqual(compareOptions.IgnoreCaseChanges, docOriginal.Revisions.Any(s => s.ParentNode.GetText().Contains("hello")));
Assert.AreNotEqual(compareOptions.IgnoreComments, docOriginal.Revisions.Any(rev => HasParentOfType(rev, NodeType.Comment)));
Assert.AreNotEqual(compareOptions.IgnoreTables, docOriginal.Revisions.Any(rev => HasParentOfType(rev, NodeType.Table)));
Assert.AreNotEqual(compareOptions.IgnoreFields, docOriginal.Revisions.Any(rev => HasParentOfType(rev, NodeType.FieldStart)));
Assert.AreNotEqual(compareOptions.IgnoreFootnotes, docOriginal.Revisions.Any(rev => HasParentOfType(rev, NodeType.Footnote)));
Assert.AreNotEqual(compareOptions.IgnoreTextboxes, docOriginal.Revisions.Any(rev => HasParentOfType(rev, NodeType.Shape)));
Assert.AreNotEqual(compareOptions.IgnoreHeadersAndFooters, docOriginal.Revisions.Any(rev => HasParentOfType(rev, NodeType.HeaderFooter)));