문서 비교

문서 비교는 두 문서 간의 변경 사항을 식별하고 변경 사항을 수정으로 포함하는 프로세스입니다. 이 프로세스는 하나의 특정 문서의 버전을 포함하여 두 문서를 비교 한 다음 두 문서 간의 변경 사항이 첫 번째 문서의 수정으로 표시됩니다.

비교 방법은 문자 수준이나 단어 수준에서 단어를 비교함으로써 달성됩니다. 단어에 하나 이상의 문자가 포함된 경우 결과에서 차이는 문자가 아닌 전체 단어의 변경으로 표시됩니다. 이 비교 과정은 법률 및 금융 산업에서 일반적인 작업입니다.

문서 간 또는 서로 다른 버전의 차이점을 수동으로 검색하는 대신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-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메서드를 두 문서에 간단히 적용하는 방법을 보여 줍니다:

// 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, 그리고 다른 사람들은"진실"으로 설정합니다.

또한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");