เปรียบเทียบเอกสาร

การเปรียบเทียบเอกสารเป็นกระบวนการที่ระบุการเปลี่ยนแปลงระหว่างเอกสารสองฉบับและมีการเปลี่ยนแปลงเป็นการแก้ไข กระบวนการนี้เปรียบเทียบเอกสารสองฉบับใดๆ รวมถึงเวอร์ชันของเอกสารเฉพาะฉบับเดียว จากนั้นการเปลี่ยนแปลงระหว่างเอกสารทั้งสองจะแสดงเป็นการแก้ไขในเอกสารฉบับแรก

วิธีการเปรียบเทียบทำได้โดยการเปรียบเทียบคำในระดับตัวอักษรหรือระดับคำ หากคำมีการเปลี่ยนแปลงอย่างน้อยหนึ่งอักขระ ผลลัพธ์ที่ได้จะแสดงเป็นการเปลี่ยนแปลงของทั้งคำ ไม่ใช่อักขระ กระบวนการเปรียบเทียบนี้เป็นงานปกติในอุตสาหกรรมด้านกฎหมายและการเงิน

แทนที่จะค้นหาความแตกต่างระหว่างเอกสารหรือเวอร์ชันต่างๆ ด้วยตนเอง คุณสามารถใช้ 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());

ระบุตัวเลือกการเปรียบเทียบขั้นสูง

มีคุณสมบัติต่างๆ มากมายของคลาส 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)));