Porównaj dokumenty

Porównywanie dokumentów jest procesem, który identyfikuje zmiany pomiędzy dwoma dokumentami i zawiera zmiany jako zmiany. Proces ten porównuje dwa dokumenty, w tym wersje jednego konkretnego dokumentu, a zmiany pomiędzy dwoma dokumentami zostaną przedstawione jako poprawki w pierwszym dokumencie.

Metoda porównania jest osiągana przez porównanie słów na poziomie znaku lub na poziomie słowa. Jeśli słowo zawiera zmianę co najmniej jednego znaku, w wyniku, różnica będzie wyświetlana jako zmiana całego słowa, a nie znaku. Ten proces porównywania jest zwykłym zadaniem w branży prawno-finansowej.

Zamiast ręcznie szukać różnic między dokumentami lub pomiędzy ich wersjami, możesz użyć Aspose.Words do porównywania dokumentów i uzyskiwania zmian treści w formatowaniu, nagłówku / stopce, tabelach i innych.

Ten artykuł wyjaśnia jak porównywać dokumenty i jak określić zaawansowane właściwości porównujące.

Ograniczenia i obsługiwane formaty plików

Porównywanie dokumentów jest bardzo złożoną cechą. Istnieją różne części kombinacji treści, które należy przeanalizować, aby rozpoznać wszystkie różnice. Powodem tej złożoności jest to, że Aspose.Words ma na celu uzyskanie takich samych wyników porównania jak Microsoft Word algorytm porównawczy.

Ogólne ograniczenie porównywania dwóch dokumentów polega na tym, że nie mogą one mieć korekt przed wywołaniem metody porównania, ponieważ ograniczenie to istnieje w Microsoft Word.

Porównaj dwa dokumenty

Kiedy porównujesz dokumenty, różnice w tym ostatnim dokumencie od poprzedniego pojawiają się jako poprawki do pierwszego. Po zmianie dokumentu, każda edycja będzie miała własną wersję po uruchomieniu metody porównania.

Aspose.Words pozwala na identyfikację różnic w dokumentach za pomocą Compare metoda - to jest podobne do Microsoft Word funkcja porównywania dokumentów. Pozwala na sprawdzanie dokumentów lub wersji dokumentów, aby znaleźć różnice i zmiany, w tym formatowanie modyfikacji, takich jak zmiany czcionki, zmiany odstępów, dodanie słów i akapitów.

W wyniku porównania dokumenty można określić jako równe lub nie równe. Określenie “równe” oznacza, że metoda porównawcza nie może przedstawiać zmian jako korekt. Oznacza to, że zarówno tekst dokumentu, jak i formatowanie tekstu są takie same. Ale mogą być inne różnice między dokumentami. Na przykład: Microsoft Word obsługuje tylko poprawki formatowe dla stylów i nie możesz reprezentować wprowadzania / usuwania stylu. Więc dokumenty mogą mieć inny zestaw stylów, a Compare metoda nadal nie powoduje żadnych zmian.

Poniższy przykład kodu pokazuje, jak sprawdzić, czy dwa dokumenty są równe lub nie:

// 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");

Poniższy przykład kodu pokazuje, jak po prostu stosować Compare metoda do dwóch dokumentów:

// 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");

Określić zaawansowane opcje porównania

Istnieje wiele różnych właściwości CompareOptions klasy, które można zastosować, gdy chcesz porównać dokumenty.

Na przykład: Aspose.Words pozwala ignorować zmiany dokonane podczas operacji porównawczej dla niektórych typów obiektów w oryginalnym dokumencie. Możesz wybrać odpowiednią właściwość dla typu obiektu, np. IgnoreHeadersAndFooters, IgnoreFormatting, IgnoreComments, i innych poprzez ustawienie ich na “true”.

Ponadto, Aspose.Words zapewnia Granularity właściwość, z którą można określić, czy śledzić zmiany według znaku lub słowa.

Inną wspólną właściwością jest wybór, w którym dokument pokazać zmiany porównania. Na przykład, “Porównywanie dialogu dokumentów” w Microsoft Word ma opcję “Pokaż zmiany w” - to również wpływa na wyniki porównania. Aspose.Words zapewnia Target nieruchomości, które służą temu celowi.

Poniższy przykład kodu pokazuje jak ustawić zaawansowane właściwości porównujące:

// 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");