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

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

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

คุณสามารถใช้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-C
System::SharedPtr<Document> docA = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
System::SharedPtr<Document> docB = System::MakeObject<Document>(inputDataDir + u"TestFile - Copy.doc");
// DocA now contains changes as revisions.
docA->Compare(docB, u"user", System::DateTime::get_Now());
if (docA->get_Revisions()->get_Count() == 0)
{
std::cout << "Documents are equal" << std::endl;
}
else
{
std::cout << "Documents are not equal" << std::endl;
}

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเพียงแค่ใช้วิธีการCompareกับสองเอกสาร:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The source document doc1
System::SharedPtr<Document> doc1 = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc1);
builder->Writeln(u"This is the original document.");
// The target document doc2
System::SharedPtr<Document> doc2 = System::MakeObject<Document>();
builder = System::MakeObject<DocumentBuilder>(doc2);
builder->Writeln(u"This is the edited document.");
// If either document has a revision, an exception will be thrown
if (doc1->get_Revisions()->get_Count() == 0 && doc2->get_Revisions()->get_Count() == 0)
doc1->Compare(doc2, u"authorName", System::DateTime::get_Now());
// If doc1 and doc2 are different, doc1 now has some revisions after the comparison, which can now be viewed and processed
if (doc1->get_Revisions()->get_Count() == 2)
std::cout << "Documents are equal." << std::endl << std::endl;
for (System::SharedPtr<Revision> r : System::IterateOver(doc1->get_Revisions()))
{
std::cout << "Revision type: " << System::ObjectExt::ToString(r->get_RevisionType()).ToUtf8String()
<< ", on a node of type " << System::ObjectExt::ToString(r->get_ParentNode()->get_NodeType()).ToUtf8String() << std::endl;
std::cout << "Changed text: " << r->get_ParentNode()->GetText() << std::endl;
}
// All the revisions in doc1 are differences between doc1 and doc2, so accepting them on doc1 transforms doc1 into doc2
doc1->get_Revisions()->AcceptAll();
// doc1, when saved, now resembles doc2
doc1->Save(inputDataDir + u"Document.Compare.docx");
doc1 = System::MakeObject<Document>(inputDataDir + u"Document.Compare.docx");
if (doc1->get_Revisions()->get_Count() == 0)
std::cout << "Documents are equal" << std::endl;
if (doc2->GetText().Trim() == doc1->GetText().Trim())
std::cout << "Documents are equal" << std::endl;

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

มีคุณสมบัติที่แตกต่างกันของCompareOptionsคลาสที่คุณสามารถใช้เมื่อคุณต้องการเปรียบเทียบเอกสาร.

ตัวอย่างเช่นAspose.Wordsอนุญาตให้คุณละเว้นการเปลี่ยนแปลงที่เกิดขึ้นในระหว่างการดำเนินการเ คุณสามารถเลือกคุณสมบัติที่เหมาะสมสำหรับประเภทออบเจกต์เช่นIgnoreHeadersAndFooters, IgnoreFormatting, IgnoreComments, และคนอื่นๆโดยการตั้งค่าให้"จริง".

นอกจากนี้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-C
// Create the original document
System::SharedPtr<Document> docOriginal = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(docOriginal);
// Insert paragraph text with an endnote
builder->Writeln(u"Hello world! This is the first paragraph.");
builder->InsertFootnote(FootnoteType::Endnote, u"Original endnote text.");
// Insert a table
builder->StartTable();
builder->InsertCell();
builder->Write(u"Original cell 1 text");
builder->InsertCell();
builder->Write(u"Original cell 2 text");
builder->EndTable();
// Insert a textbox
System::SharedPtr<Shape> textBox = builder->InsertShape(ShapeType::TextBox, 150, 20);
builder->MoveTo(textBox->get_FirstParagraph());
builder->Write(u"Original textbox contents");
// Insert a DATE field
builder->MoveTo(docOriginal->get_FirstSection()->get_Body()->AppendParagraph(u""));
builder->InsertField(u" DATE ");
// Insert a comment
auto newComment = System::MakeObject<Comment>(docOriginal, u"John Doe", u"J.D.", System::DateTime::get_Now());
newComment->SetText(u"Original comment.");
builder->get_CurrentParagraph()->AppendChild(newComment);
// Insert a header
builder->MoveToHeaderFooter(Aspose::Words::HeaderFooterType::HeaderPrimary);
builder->Writeln(u"Original header contents.");
// Create a clone of our document, which we will edit and later compare to the original
auto docEdited = System::DynamicCast<Aspose::Words::Document>(System::StaticCast<Node>(docOriginal)->Clone(true));
System::SharedPtr<Paragraph> firstParagraph = docEdited->get_FirstSection()->get_Body()->get_FirstParagraph();
// Change the formatting of the first paragraph, change casing of original characters and add text
firstParagraph->get_Runs()->idx_get(0)->set_Text(u"hello world! this is the first paragraph, after editing.");
firstParagraph->get_ParagraphFormat()->set_Style(docEdited->get_Styles()->idx_get(Aspose::Words::StyleIdentifier::Heading1));
// Edit the footnote
auto footnote = System::DynamicCast<Aspose::Words::Footnote>(docEdited->GetChild(Aspose::Words::NodeType::Footnote, 0, true));
footnote->get_FirstParagraph()->get_Runs()->idx_get(1)->set_Text(u"Edited endnote text.");
// Edit the table
auto table = System::DynamicCast<Aspose::Words::Tables::Table>(docEdited->GetChild(Aspose::Words::NodeType::Table, 0, true));
table->get_FirstRow()->get_Cells()->idx_get(1)->get_FirstParagraph()->get_Runs()->idx_get(0)->set_Text(u"Edited Cell 2 contents");
// Edit the textbox
textBox = System::DynamicCast<Aspose::Words::Drawing::Shape>(docEdited->GetChild(Aspose::Words::NodeType::Shape, 0, true));
textBox->get_FirstParagraph()->get_Runs()->idx_get(0)->set_Text(u"Edited textbox contents");
// Edit the DATE field
auto fieldDate = System::DynamicCast<Aspose::Words::Fields::FieldDate>(docEdited->get_Range()->get_Fields()->idx_get(0));
fieldDate->set_UseLunarCalendar(true);
// Edit the comment
auto comment = System::DynamicCast<Aspose::Words::Comment>(docEdited->GetChild(Aspose::Words::NodeType::Comment, 0, true));
comment->get_FirstParagraph()->get_Runs()->idx_get(0)->set_Text(u"Edited comment.");
// Edit the header
docEdited->get_FirstSection()->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType::HeaderPrimary)->get_FirstParagraph()->get_Runs()->idx_get(0)->set_Text(u"Edited header contents.");
// When we compare documents, the differences of the latter document from the former show up as revisions to the former
// Each edit that we've made above will have its own revision, after we run the Compare method
// We can compare with a CompareOptions object, which can suppress changes done to certain types of objects within the original document
// from registering as revisions after the comparison by setting some of these members to "true"
auto compareOptions = System::MakeObject<Aspose::Words::CompareOptions>();
compareOptions->set_IgnoreFormatting(false);
compareOptions->set_IgnoreCaseChanges(false);
compareOptions->set_IgnoreComments(false);
compareOptions->set_IgnoreTables(false);
compareOptions->set_IgnoreFields(false);
compareOptions->set_IgnoreFootnotes(false);
compareOptions->set_IgnoreTextboxes(false);
compareOptions->set_IgnoreHeadersAndFooters(false);
compareOptions->set_Target(Aspose::Words::ComparisonTargetType::New);
docOriginal->Compare(docEdited, u"John Doe", System::DateTime::get_Now(), compareOptions);
docOriginal->Save(inputDataDir + u"Document.CompareOptions.docx");