เปรียบเทียบเอกสาร
การเปรียบเทียบเอกสารเป็นกระบวนการที่ระบุการเปลี่ยนแปลงระหว่างสองเอกสารแ กระบวนการนี้เปรียบเทียบเอกสารสองเอกสารใดๆรวมถึงรุ่นของเอกสารที่เฉพาะเจาะจงหนึ่งจากนั้นการเปลี่ยนแปลงระหว่างเอกสารทั้งสองจะแสดงเป็นการแก้ไขในเอกสารแรก.
วิธีการเปรียบเทียบจะทำได้โดยการเปรียบเทียบคำในระดับตัวอักษรหรือที่ระดับคำ ถ้าคำมีการเปลี่ยนแปลงอย่างน้อยหนึ่งตัวอักษรในผลลัพธ์ความแตกต่างจะแสดงเป็นการเ กระบวนการของการเปรียบเทียบนี้เป็นงานปกติในอุตสาหกรรมทางกฎหมายและการเงิน.
คุณสามารถใช้Aspose.Wordsเพื่อเปรียบเทียบเอกสารและรับการเปลี่ยนแปลงเนื้อหาในการจัดรูปแบบ.
บทความนี้อธิบายวิธีการเปรียบเทียบเอกสารและวิธีการระบุคุณสมบัติการเปรียบเทียบขั้นสู.
ลองออนไลน์
คุณสามารถเปรียบเทียบสองเอกสารออนไลน์โดยใช้ เปรียบเทียบเอกสารออนไลน์ กเขา.
โปรดทราบว่าวิธีการเปรียบเทียบ,อธิบายไว้ด้านล่าง,ถูกนำมาใช้ในเครื่องมือนี้เพื่อให้ได้ผลลัพธ์ที่เท่าเทียมกัน. ดังนั้นคุณจะได้รับผลลัพธ์เดียวกันแม้โดยใช้เครื่องมือเปรียบเทียบออนไลน์หรือโดยใช้วิธีการเปรียบเทียบใน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"); |
ระบุตัวเลือกการเปรียบเทียบขั้นสูง
มีคุณสมบัติที่แตกต่างกันของ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"); |