文書の比較
ドキュメントの比較は、2つのドキュメント間の変更を識別し、変更をリビジョンとして含むプロセスです。 このプロセスでは、特定の文書のバージョンを含む任意の二つの文書を比較し、両方の文書間の変更は、最初の文書のリビジョンとして表示されます。
比較方法は、文字レベルまたは単語レベルで単語を比較することによって達成される。 単語に少なくとも一つの文字の変更が含まれている場合、結果では、違いは文字ではなく単語全体の変更として表示されます。 この比較プロセスは、法律および金融業界では通常の作業です。
ドキュメント間または異なるバージョン間の違いを手動で検索する代わりに、Aspose.Wordsを使用してドキュメントを比較し、書式、ヘッダー/フッター、テーブルなどのコンテ
この記事では、ドキュメントを比較する方法と、高度な比較プロパティを指定する方法について説明します。
オンラインで試す
を使用してオンラインで二つの文書を比較することができます オンラインでの文書比較 ツール。
このツールでは、同じ結果を得るために、以下で説明する比較方法が使用されていることに注意してください。 したがって、オンライン比較ツールを使用するか、Aspose.Wordsの比較方法を使用しても同じ結果が得られます。
制限事項とサポートされているファイル形式
文書の比較は非常に複雑な機能です。 コンテンツの組み合わせには、すべての違いを認識するために分析する必要があるさまざまな部分があります。 この複雑さの理由は、Aspose.WordsがMicrosoft Word比較アルゴリズムと同じ比較結果を得ることを目的としているという事実によるものです。
比較される二つの文書の一般的な制限は、この制限がMicrosoft Wordに存在するため、compareメソッドを呼び出す前にリビジョンを持ってはならないということです。
二つの文書を比較する
ドキュメントを比較すると、後者のドキュメントと前者のドキュメントの違いが前者のリビジョンとして表示されます。 ドキュメントを変更すると、compareメソッドを実行した後、各編集には独自のリビジョンがあります。
Aspose.WordsはCompareメソッドを使用して文書の違いを識別できます–これはMicrosoft Word文書比較機能に似ています。 文書や文書のバージョンを確認して、フォントの変更、間隔の変更、単語や段落の追加などの書式設定の変更など、違いや変更を見つけることができます。
比較の結果、文書は等しいか等しくないかを決定することができる。 「等しい」文書という用語は、比較方法が変更をリビジョンとして表すことができないことを意味します。 これは、ドキュメントのテキストとテキストの書式設定の両方が同じであることを意味します。 ただし、ドキュメント間には他にも違いがある可能性があります。 たとえば、Microsoft Wordはスタイルの書式変更のみをサポートし、スタイルの挿入/削除を表すことはできません。 したがって、文書は異なるスタイルのセットを持つことができ、Compareメソッドはまだリビジョンを生成しません。
次のコード例は、2つのドキュメントが等しいかどうかを確認する方法を示しています:
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
メソッドを2つのドキュメントに適用する方法を示しています:
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; | |
高度な比較オプション{#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-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"); |