文書の比較
ドキュメントの比較は、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-Java | |
Document docA = new Document(dataDir + "DocumentA.doc"); | |
Document docB = new Document(dataDir + "DocumentB.doc"); | |
docA.compare(docB, "user", new Date()); | |
if (docA.getRevisions().getCount() == 0) | |
System.out.println("Documents are equal"); | |
else | |
System.out.println("Documents are not equal"); |
次のコード例は、単純にCompare
メソッドを2つのドキュメントに適用する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// 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.getRevisions().getCount() == 0 && doc2.getRevisions().getCount() == 0) | |
doc1.compare(doc2, "authorName", new Date()); | |
// If doc1 and doc2 are different, doc1 now has some revisions after the comparison, which can now be viewed and processed | |
if (doc1.getRevisions().getCount() == 2) | |
System.out.println("Documents are equal"); | |
for (Revision r : doc1.getRevisions()) | |
{ | |
System.out.println("Revision type: " + r.getRevisionType() + ", on a node of type " + r.getParentNode().getNodeType() + ""); | |
System.out.println("\tChanged text: " + r.getParentNode().getText() + ""); | |
} | |
// All the revisions in doc1 are differences between doc1 and doc2, so accepting them on doc1 transforms doc1 into doc2 | |
doc1.getRevisions().acceptAll(); | |
// doc1, when saved, now resembles doc2 | |
doc1.save(dataDir + "Document.Compare.docx"); | |
doc1 = new Document(dataDir + "Document.Compare.docx"); | |
if (doc1.getRevisions().getCount() == 0) | |
System.out.println("Documents are equal"); | |
if (doc2.getText().trim() == doc1.getText().trim()) | |
System.out.println("Documents are equal"); |
高度な比較オプション {#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-Java | |
// 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.TEXT_BOX, 150, 20); | |
builder.moveTo(textBox.getFirstParagraph()); | |
builder.write("Original textbox contents"); | |
// Insert a DATE field | |
builder.moveTo(docOriginal.getFirstSection().getBody().appendParagraph("")); | |
builder.insertField(" DATE "); | |
// Insert a comment | |
Comment newComment = new Comment(docOriginal, "John Doe", "J.D.", new Date()); | |
newComment.setText("Original comment."); | |
builder.getCurrentParagraph().appendChild(newComment); | |
// Insert a header | |
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY); | |
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.deepClone(true); | |
Paragraph firstParagraph = docEdited.getFirstSection().getBody().getFirstParagraph(); | |
// Change the formatting of the first paragraph, change casing of original characters and add text | |
firstParagraph.getRuns().get(0).setText("hello world! this is the first paragraph, after editing."); | |
firstParagraph.getParagraphFormat().setStyle(docEdited.getStyles().get(StyleIdentifier.HEADING_1)); | |
// Edit the footnote | |
Footnote footnote = (Footnote)docEdited.getChild(NodeType.FOOTNOTE, 0, true); | |
footnote.getFirstParagraph().getRuns().get(1).setText("Edited endnote text."); | |
// Edit the table | |
Table table = (Table)docEdited.getChild(NodeType.TABLE, 0, true); | |
table.getFirstRow().getCells().get(1).getFirstParagraph().getRuns().get(0).setText("Edited Cell 2 contents"); | |
// Edit the textbox | |
textBox = (Shape)docEdited.getChild(NodeType.SHAPE, 0, true); | |
textBox.getFirstParagraph().getRuns().get(0).setText("Edited textbox contents"); | |
// Edit the DATE field | |
FieldDate fieldDate = (FieldDate)docEdited.getRange().getFields().get(0); | |
fieldDate.setUseLunarCalendar(true); | |
// Edit the comment | |
Comment comment = (Comment)docEdited.getChild(NodeType.COMMENT, 0, true); | |
comment.getFirstParagraph().getRuns().get(0).setText("Edited comment."); | |
// Edit the header | |
docEdited.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY).getFirstParagraph().getRuns().get(0).setText("Edited header contents."); | |
// Apply different comparing options | |
CompareOptions compareOptions = new CompareOptions(); | |
compareOptions.setIgnoreFormatting(false); | |
compareOptions.setIgnoreCaseChanges(false); | |
compareOptions.setIgnoreComments(false); | |
compareOptions.setIgnoreTables(false); | |
compareOptions.setIgnoreFields(false); | |
compareOptions.setIgnoreFootnotes(false); | |
compareOptions.setIgnoreTextboxes(false); | |
compareOptions.setIgnoreHeadersAndFooters(false); | |
compareOptions.setTarget(ComparisonTargetType.NEW); | |
// compare both documents | |
docOriginal.compare(docEdited, "John Doe", new Date(), compareOptions); | |
docOriginal.save(dataDir + "Document.CompareOptions.docx"); |