ติดตามการเปลี่ยนแปลงในเอกสาร

ฟังก์ชันการทำงานของการเปลี่ยนแปลงแทร็กหรือที่เรียกว่าการตรวจทานช่วยให้คุณสามา คุณลักษณะการเปลี่ยนแปลงแทร็คนี้ด้วยAspose.Wordsรองรับการเปลี่ยนแปลงแทร็คในMicrosoft Word ด้วยฟังก์ชันการทำงานนี้คุณสามารถเข้าถึงการแก้ไขแต่ละรายการในเอกสารของคุณและ.

เมื่อคุณเปิดใช้งานคุณลักษณะการเปลี่ยนแปลงแทร็กองค์ประกอบทั้งหมดที่แทรกลบและแก้ไข วัตถุที่นำข้อมูลเกี่ยวกับสิ่งที่เปลี่ยนแปลงจะเรียกว่า"การติดตามการเปลี่ยนแปลง" ตัวอย่างเช่นสมมติว่าคุณต้องการตรวจสอบเอกสารและทำการเปลี่ยนแปลงที่สำคัญ-นี้อาจ นอกจากนี้คุณอาจต้องแทรกความคิดเห็นเพื่อหารือเกี่ยวกับการเปลี่ยนแปลงบางอย่าง นั่นคือที่ที่การติดตามการเปลี่ยนแปลงในเอกสารมาใน.

บทความนี้อธิบายถึงวิธีจัดการและติดตามการเปลี่ยนแปลงที่สร้างขึ้นโดยผู้ตรวจทานจำนว.

การแก้ไขคืออะไร

ก่อนที่จะดำน้ำในการแก้ไขขออธิบายความหมายของการแก้ไข revisionคือการเปลี่ยนแปลงที่เกิดขึ้นในโหนดหนึ่งของเอกสารในขณะที่กลุ่มการแก้ไขแสดงโดยRevisionGroupคื การแก้ไขเป็นเครื่องมือสำหรับการติดตามการเปลี่ยนแปลง.

การแก้ไขจะใช้ในคุณลักษณะการเปลี่ยนแปลงการติดตามและภายในคุณลักษณะเปรียบเทีย ดังนั้นการแก้ไขภายในคุณลักษณะการเปลี่ยนแปลงการติดตามจะแสดงโดยใครและสิ่งที่ได้.

Aspose.Wordsรองรับประเภทการแก้ไขที่แตกต่างกันเช่นเดียวกับในMicrosoft Wordเช่นการแทรกการลบFormatChange,StyleDefinitionChangeและการย้าย ประเภทการแก้ไขทั้งหมดจะแสดงด้วยการแจงนับRevisionType.

เริ่มและหยุดการติดตามการเปลี่ยนแปลง

การแก้ไขเอกสารมักจะไม่นับเป็นการแก้ไขจนกว่าคุณจะเริ่มติดตาม Aspose.Wordsช่วยให้คุณสามารถติดตามการเปลี่ยนแปลงทั้งหมดในเอกสารของคุณโดยอัตโนมัติด้วยขั้น คุณสามารถเริ่มกระบวนการติดตามการเปลี่ยนแปลงได้โดยใช้วิธีการStartTrackRevisions การแก้ไขในอนาคตไม่ได้รับการพิจารณาการแก้ไขคุณจะต้องใช้วิธีการStopTrackRevisions.

ในตอนท้ายของกระบวนการการติดตามการเปลี่ยนแปลงในเอกสารของคุณ,คุณจะมีความส นี้สามารถทำได้โดยใช้วิธีการAcceptAllRevisionsหรือRejectAll นอกจากนี้คุณสามารถยอมรับหรือปฏิเสธการแก้ไขแต่ละครั้งแยกกันได้โดยใช้วิธีการAcceptหรือReject.

การเปลี่ยนแปลงทั้งหมดจะได้รับการติดตามสำหรับหนึ่งซ้ำจากช่วงเวลาที่คุณเริ่มต้นกระบ การเชื่อมต่อระหว่างการทำซ้ำที่แตกต่างกันจะแสดงเป็นสถานการณ์สมมติต่อไปนี้:คุณทำกระ ด้วยสถานการณ์สมมตินี้การเปลี่ยนแปลงทั้งหมดที่คุณไม่ยอมรับหรือปฏิเสธจะปรากฏขึ้นอีกค.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำงานกับการเปลี่ยนแปลงการติดตาม:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
Body body = doc.getFirstSection().getBody();
Paragraph para = body.getFirstParagraph();
// Add text to the first paragraph, then add two more paragraphs.
para.appendChild(new Run(doc, "Paragraph 1. "));
body.appendParagraph("Paragraph 2. ");
body.appendParagraph("Paragraph 3. ");
// We have three paragraphs, none of which registered as any type of revision
// If we add/remove any content in the document while tracking revisions,
// they will be displayed as such in the document and can be accepted/rejected.
doc.startTrackRevisions("John Doe", new Date());
// This paragraph is a revision and will have the according "IsInsertRevision" flag set.
para = body.appendParagraph("Paragraph 4. ");
if(para.isInsertRevision())
System.out.println("isInsertRevision:" + para.isInsertRevision());
// Get the document's paragraph collection and remove a paragraph.
ParagraphCollection paragraphs = body.getParagraphs();
if(4 == paragraphs.getCount())
System.out.println("count:" + paragraphs.getCount());
para = paragraphs.get(2);
para.remove();
// Since we are tracking revisions, the paragraph still exists in the document, will have the "IsDeleteRevision" set
// and will be displayed as a revision in Microsoft Word, until we accept or reject all revisions.
if(4 == paragraphs.getCount())
System.out.println("count:" + paragraphs.getCount());
if(para.isDeleteRevision())
System.out.println("isDeleteRevision:" + para.isDeleteRevision());
// The delete revision paragraph is removed once we accept changes.
doc.acceptAllRevisions();
if(3 == paragraphs.getCount())
System.out.println("count:" + paragraphs.getCount());
// Stopping the tracking of revisions makes this text appear as normal text.
// Revisions are not counted when the document is changed.
doc.stopTrackRevisions();
// Save the document.
doc.save(dataDir + "Document.Revisions.docx");

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างการแก้ไขเมื่อโหนดถูกย้ายภายในเอกสารที่ติดตาม:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Generate document contents.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Paragraph 1");
builder.writeln("Paragraph 2");
builder.writeln("Paragraph 3");
builder.writeln("Paragraph 4");
builder.writeln("Paragraph 5");
builder.writeln("Paragraph 6");
Body body = doc.getFirstSection().getBody();
System.out.println("Paragraph count:" + body.getParagraphs().getCount());
// Start tracking revisions.
doc.startTrackRevisions("Author", new Date());
// Generate revisions when moving a node from one location to another.
Node node = body.getParagraphs().get(3);
Node endNode = body.getParagraphs().get(5).getNextSibling();
Node referenceNode = body.getParagraphs().get(0);
while (node != endNode)
{
Node nextNode = node.getNextSibling();
body.insertBefore(node, referenceNode);
node = nextNode;
}
// Stop the process of tracking revisions.
doc.stopTrackRevisions();
// There are 3 additional paragraphs in the move-from range.
System.out.println("Paragraph count: " + body.getParagraphs().getCount());
doc.save(dataDir + "out.docx");

จัดการและจัดเก็บการเปลี่ยนแปลงเป็นการแก้ไข

ด้วยคุณลักษณะการเปลี่ยนแปลงการติดตามก่อนหน้านี้คุณสามารถเข้าใจว่ามีการเปลี่ยนแป ในขณะที่มีคุณลักษณะTrackRevisionsคุณบังคับให้มีการเปลี่ยนแปลงใดๆในเอกสารของคุณจะถูกเก็บไว้เป็นก.

Aspose.Wordsช่วยให้คุณสามารถตรวจสอบว่าเอกสารมีการแก้ไขหรือไม่โดยใช้คุณสมบัติHasRevision หากคุณไม่จำเป็นต้องติดตามการเปลี่ยนแปลงในเอกสารของคุณโดยอัตโนมัติผ่านวิธีการStartTrackRevisionsและStopTrackRevisionsคุณสามารถใช้คุณสมบัติTrackRevisionsเพื่อตรวจสอบว่ามีการติดตามการเปลี่ยนแปลงในขณะที่แก้ไขเอกสารในMicrosoft Wordและจัดเก็บไว้เป็นการแก้ไขหรือไม่.

คุณลักษณะTrackRevisionsทำการแก้ไขแทนการเปลี่ยนแปลงจริงDOM แต่การแก้ไขตัวเองแยกกัน ตัวอย่างเช่นถ้าคุณลบย่อหน้าใดๆให้เป็นAspose.Wordsแก้ไขให้ทำเครื่องหมายว่าเป็นลบแทนที่จะลบย่อหน้า.

นอกจากนี้Aspose.Wordsช่วยให้คุณสามารถตรวจสอบว่าวัตถุถูกแทรก,ลบ,หรือเปลี่ยนรูปแบบโดยใช้การIsDeleteRevision, IsFormatRevision, IsInsertRevision, IsMoveFromRevision, และคุณสมบัติIsMoveToRevision.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการใช้คุณสมบัติที่แตกต่างกันกับการแก้ไข:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open a blank document.
Document doc = new Document();
// Insert an inline shape without tracking revisions.
Shape shape = new Shape(doc, ShapeType.CUBE);
shape.setWrapType(WrapType.INLINE);
shape.setWidth(100.0);
shape.setHeight(100.0);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(shape);
// Start tracking revisions and then insert another shape.
doc.startTrackRevisions("John Doe");
shape = new Shape(doc, ShapeType.SUN);
shape.setWrapType(WrapType.INLINE);
shape.setWidth(100.0);
shape.setHeight(100.0);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(shape);
// Get the document's shape collection which includes just the two shapes we added.
Node[] shapes = doc.getChildNodes(NodeType.SHAPE, true).toArray();
if(2 == shapes.length)
System.out.println("Shapes Count:" + shapes.length);
// Remove the first shape.
shapes[0].remove();
Shape sh = (Shape) shapes[0];
// Because we removed that shape while changes were being tracked, the shape counts as a delete revision.
if(ShapeType.CUBE == sh.getShapeType())
System.out.println("Shape is CUBE");
if(sh.isDeleteRevision())
System.out.println("isDeleteRevision:" + sh.isDeleteRevision());
// And we inserted another shape while tracking changes, so that shape will count as an insert revision.
sh = (Shape) shapes[1];
if(ShapeType.SUN == sh.getShapeType())
System.out.println("Shape is SUN");
if(sh.isInsertRevision())
System.out.println("IsInsertRevision:" + sh.isInsertRevision());
// The document has one shape that was moved, but shape move revisions will have two instances of that shape.
// One will be the shape at its arrival destination and the other will be the shape at its original location.
doc = new Document(dataDir + "Revision shape.docx");
Node[] nc = doc.getChildNodes(NodeType.SHAPE, true).toArray();
if(4 == nc.length)
System.out.println("Shapes Count:" + nc.length);
Shape mvr = (Shape) nc[0];
// This is the move to revision, also the shape at its arrival destination.
if(mvr.isMoveFromRevision())
System.out.println("isMoveFromRevision:" + mvr.isMoveFromRevision());
if(mvr.isMoveToRevision())
System.out.println("isMoveToRevision:" + mvr.isMoveToRevision());
mvr = (Shape) nc[1];
// This is the move from revision, which is the shape at its original location.
if(mvr.isMoveFromRevision())
System.out.println("isMoveFromRevision:" + mvr.isMoveFromRevision());
if(mvr.isMoveToRevision())
System.out.println("isMoveToRevision:" + mvr.isMoveToRevision());