文書内の変更を追跡する

変更履歴の機能(レビューとも呼ばれます)を使用すると、自分や他のユーザーが行ったコンテンツや書式設定の変更を追跡できます。 Aspose.Wordsのこのトラック変更機能はMicrosoft Wordのトラック変更をサポートします。 この機能を使用すると、ドキュメント内の個々のリビジョンにアクセスし、それらに異なるプロパティを適用できます。

変更の追跡機能を有効にすると、文書の挿入、削除、および変更されたすべての要素が、誰が、いつ、何が変更されたかに関する情報で視覚的に強調表示さ 何が変更されたかに関する情報を保持するオブジェクトは、“変更の追跡"と呼ばれます。 たとえば、文書を確認して重要な変更を加えるとします。 また、いくつかの変更について議論するためにコメントを挿入する必要がある場合があります。 ここで、文書の変更を追跡することができます。

この記事では、同じ文書で多くの査読者が作成した変更を管理および追跡する方法と、変更を追跡するためのプロパティについて説明します。

リビジョンとは何ですか

リビジョンに飛び込む前に、リビジョンの意味を説明しましょう。 revisionはドキュメントの1つのノードで発生する変更ですが、RevisionGroupクラスで表されるリビジョングループは、ドキュメントの多くのノードで発生する連続した リビジョンは、変更を追跡するためのツールです。

リビジョンは、変更の追跡機能およびドキュメントの比較機能内で使用され、比較の結果としてリビジョンが表示されます。 そのため、変更の追跡機能内のリビジョンには、誰が何を変更したかが表示されます。

Aspose.Wordsは、挿入、削除、FormatChange、StyleDefinitionChange、移動など、Microsoft Wordと同様に、さまざまなリビジョンタイプをサポートします。 すべてのリビジョンの種類はRevisionType列挙体で表されます。

変更の追跡の開始と停止

通常、文書を編集しても、追跡を開始するまではリビジョンとしてカウントされません。 Aspose.Words簡単な手順で文書内のすべての変更を自動的に追跡できます。 StartTrackRevisionsメソッドを使用すると、変更を追跡するプロセスを簡単に開始できます。 今後の編集がリビジョンと見なされないように変更の追跡プロセスを停止する必要がある場合は、StopTrackRevisionsメソッドを使用する必要があります。

文書の変更の追跡プロセスの最後に、すべてのリビジョンを承認したり、文書を元の形式に戻すために拒否したりすることもできます。 これは、AcceptAllRevisionsまたはRejectAllメソッドを使用することで実現できます。 また、AcceptまたはRejectメソッドを使用して、各リビジョンを個別に承認または拒否することができます。

すべての変更は、プロセスを開始した瞬間から停止した瞬間まで、1回の反復で追跡されます。 追跡プロセスを完了し、いくつかの変更を加えてから、変更の追跡を再度開始します。 このシナリオでは、受け入れなかった、または拒否しなかったすべての変更が再び表示されます。

次のコード例は、変更の追跡を操作する方法を示しています:

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