java.lang.Objectcom.aspose.words.Revision
public class Revision
Example:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Normal editing of the document does not count as a revision
builder.write("This does not count as a revision. ");
Assert.assertFalse(doc.hasRevisions());
// In order for our edits to count as revisions, we need to declare an author and start tracking them
doc.startTrackRevisions("John Doe", new Date());
builder.write("This is revision #1. ");
// This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually
// done there and not the programmatic changes we are about to do here
Assert.assertFalse(doc.getTrackRevisions());
// As well as nodes in the document, revisions get referenced in this collection
Assert.assertTrue(doc.hasRevisions());
Assert.assertEquals(doc.getRevisions().getCount(), 1);
Revision revision = doc.getRevisions().get(0);
Assert.assertEquals(revision.getAuthor(), "John Doe");
Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. ");
Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION);
Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date()));
Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0));
// Deleting content also counts as a revision
// The most recent revisions are put at the start of the collection
doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove();
Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION);
Assert.assertEquals(doc.getRevisions().getCount(), 2);
// Insert revisions are treated as document text by the GetText() method before they are accepted,
// since they are still nodes with text and are in the body
Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1.");
// Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection
doc.getRevisions().get(0).accept();
Assert.assertEquals(doc.getRevisions().getCount(), 1);
// Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here
Assert.assertEquals(doc.getText().trim(), "This is revision #1.");
// The second insertion revision is now at index 0, which we can reject to ignore and discard it
doc.getRevisions().get(0).reject();
Assert.assertEquals(doc.getRevisions().getCount(), 0);
Assert.assertEquals(doc.getText().trim(), "");
Property Getters/Setters Summary | ||
---|---|---|
java.lang.String | getAuthor() | |
void | setAuthor(java.lang.String value) | |
Gets or sets the author of this revision. Can not be empty string or null. | ||
java.util.Date | getDateTime() | |
void | setDateTime(java.util.Date value) | |
Gets or sets the date/time of this revision. | ||
RevisionGroup | getGroup() | |
Gets the revision group. Returns null if the revision does not belong to any group. | ||
Node | getParentNode() | |
Gets the immediate parent node (owner) of this revision.
This property will work for any revision type other than |
||
Style | getParentStyle() | |
Gets the immediate parent style (owner) of this revision.
This property will work for only for the |
||
int | getRevisionType() | |
Gets the type of this revision. The value of the property is RevisionType integer constant. |
Method Summary | ||
---|---|---|
void | accept() | |
Accepts this revision. | ||
void | reject() | |
Reject this revision. |
Property Getters/Setters Detail |
---|
getAuthor/setAuthor | |
public java.lang.String getAuthor() / public void setAuthor(java.lang.String value) |
Example:
Shows how to check if a document has revisions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normal editing of the document does not count as a revision builder.write("This does not count as a revision. "); Assert.assertFalse(doc.hasRevisions()); // In order for our edits to count as revisions, we need to declare an author and start tracking them doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); // This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually // done there and not the programmatic changes we are about to do here Assert.assertFalse(doc.getTrackRevisions()); // As well as nodes in the document, revisions get referenced in this collection Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(doc.getRevisions().getCount(), 1); Revision revision = doc.getRevisions().get(0); Assert.assertEquals(revision.getAuthor(), "John Doe"); Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. "); Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION); Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date())); Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0)); // Deleting content also counts as a revision // The most recent revisions are put at the start of the collection doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION); Assert.assertEquals(doc.getRevisions().getCount(), 2); // Insert revisions are treated as document text by the GetText() method before they are accepted, // since they are still nodes with text and are in the body Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1."); // Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection doc.getRevisions().get(0).accept(); Assert.assertEquals(doc.getRevisions().getCount(), 1); // Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here Assert.assertEquals(doc.getText().trim(), "This is revision #1."); // The second insertion revision is now at index 0, which we can reject to ignore and discard it doc.getRevisions().get(0).reject(); Assert.assertEquals(doc.getRevisions().getCount(), 0); Assert.assertEquals(doc.getText().trim(), "");
getDateTime/setDateTime | |
public java.util.Date getDateTime() / public void setDateTime(java.util.Date value) |
Example:
Shows how to check if a document has revisions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normal editing of the document does not count as a revision builder.write("This does not count as a revision. "); Assert.assertFalse(doc.hasRevisions()); // In order for our edits to count as revisions, we need to declare an author and start tracking them doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); // This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually // done there and not the programmatic changes we are about to do here Assert.assertFalse(doc.getTrackRevisions()); // As well as nodes in the document, revisions get referenced in this collection Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(doc.getRevisions().getCount(), 1); Revision revision = doc.getRevisions().get(0); Assert.assertEquals(revision.getAuthor(), "John Doe"); Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. "); Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION); Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date())); Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0)); // Deleting content also counts as a revision // The most recent revisions are put at the start of the collection doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION); Assert.assertEquals(doc.getRevisions().getCount(), 2); // Insert revisions are treated as document text by the GetText() method before they are accepted, // since they are still nodes with text and are in the body Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1."); // Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection doc.getRevisions().get(0).accept(); Assert.assertEquals(doc.getRevisions().getCount(), 1); // Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here Assert.assertEquals(doc.getText().trim(), "This is revision #1."); // The second insertion revision is now at index 0, which we can reject to ignore and discard it doc.getRevisions().get(0).reject(); Assert.assertEquals(doc.getRevisions().getCount(), 0); Assert.assertEquals(doc.getText().trim(), "");
getGroup | |
public RevisionGroup getGroup() |
Example:
Shows how to check if a document has revisions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normal editing of the document does not count as a revision builder.write("This does not count as a revision. "); Assert.assertFalse(doc.hasRevisions()); // In order for our edits to count as revisions, we need to declare an author and start tracking them doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); // This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually // done there and not the programmatic changes we are about to do here Assert.assertFalse(doc.getTrackRevisions()); // As well as nodes in the document, revisions get referenced in this collection Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(doc.getRevisions().getCount(), 1); Revision revision = doc.getRevisions().get(0); Assert.assertEquals(revision.getAuthor(), "John Doe"); Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. "); Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION); Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date())); Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0)); // Deleting content also counts as a revision // The most recent revisions are put at the start of the collection doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION); Assert.assertEquals(doc.getRevisions().getCount(), 2); // Insert revisions are treated as document text by the GetText() method before they are accepted, // since they are still nodes with text and are in the body Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1."); // Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection doc.getRevisions().get(0).accept(); Assert.assertEquals(doc.getRevisions().getCount(), 1); // Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here Assert.assertEquals(doc.getText().trim(), "This is revision #1."); // The second insertion revision is now at index 0, which we can reject to ignore and discard it doc.getRevisions().get(0).reject(); Assert.assertEquals(doc.getRevisions().getCount(), 0); Assert.assertEquals(doc.getText().trim(), "");
getParentNode | |
public Node getParentNode() |
Example:
Shows how to view revision-related properties of Inline nodes.Document doc = new Document(getMyDir() + "Revision runs.docx"); // This document has 6 revisions Assert.assertEquals(doc.getRevisions().getCount(), 6); // The parent node of a revision is the run that the revision concerns, which is an Inline node Run run = (Run) doc.getRevisions().get(0).getParentNode(); // Get the parent paragraph Paragraph firstParagraph = run.getParentParagraph(); RunCollection runs = firstParagraph.getRuns(); Assert.assertEquals(runs.getCount(), 6); // The text in the run at index #2 was typed after revisions were tracked, so it will count as an insert revision // The font was changed, so it will also be a format revision Assert.assertTrue(runs.get(2).isInsertRevision()); Assert.assertTrue(runs.get(2).isFormatRevision()); // If one node was moved from one place to another while changes were tracked, // the node will be placed at the departure location as a "move to revision", // and a "move from revision" node will be left behind at the origin, in case we want to reject changes // Highlighting text and dragging it to another place with the mouse and cut-and-pasting (but not copy-pasting) both count as "move revisions" // The node with the "IsMoveToRevision" flag is the arrival of the move operation, and the node with the "IsMoveFromRevision" flag is the departure point Assert.assertTrue(runs.get(1).isMoveToRevision()); Assert.assertTrue(runs.get(4).isMoveFromRevision()); // If an Inline node gets deleted while changes are being tracked, it will leave behind a node with the IsDeleteRevision flag set to true until changes are accepted Assert.assertTrue(runs.get(5).isDeleteRevision());
getParentStyle | |
public Style getParentStyle() |
Example:
Shows how to look through a document's revisions.// Open a document that contains revisions and get its revision collection Document doc = new Document(getMyDir() + "Revisions.docx"); RevisionCollection revisions = doc.getRevisions(); // This collection itself has a collection of revision groups, which are merged sequences of adjacent revisions System.out.println("{revisions.Groups.Count} revision groups:"); // We can iterate over the collection of groups and access the text that the revision concerns Iterator<RevisionGroup> e = revisions.getGroups().iterator(); while (e.hasNext()) { RevisionGroup currentRevisionGroup = e.next(); System.out.println(MessageFormat.format("\tGroup type \"{0}\", ", currentRevisionGroup.getRevisionType()) + MessageFormat.format("author: {0}, contents: [{1}]", currentRevisionGroup.getAuthor(), currentRevisionGroup.getText().trim())); } // The collection of revisions is considerably larger than the condensed form we printed above, // depending on how many Runs the text has been segmented into during editing in Microsoft Word, // since each Run affected by a revision gets its own Revision object System.out.println("\n{revisions.Count} revisions:"); Iterator<Revision> e1 = revisions.iterator(); while (e1.hasNext()) { Revision currentRevision = e1.next(); // A StyleDefinitionChange strictly affects styles and not document nodes, so in this case the ParentStyle // attribute will always be used, while the ParentNode will always be null // Since all other changes affect nodes, ParentNode will conversely be in use and ParentStyle will be null if (currentRevision.getRevisionType() == RevisionType.STYLE_DEFINITION_CHANGE) { System.out.println(MessageFormat.format("\tRevision type \"{0}\", ", currentRevision.getRevisionType()) + MessageFormat.format("author: {0}, style: [{1}]", currentRevision.getAuthor(), currentRevision.getParentStyle().getName())); } else { System.out.println(MessageFormat.format("\tRevision type \"{0}\", ", currentRevision.getRevisionType()) + MessageFormat.format("author: {0}, contents: [{1}]", currentRevision.getAuthor(), currentRevision.getParentNode().getText().trim())); } } // While the collection of revision groups provides a clearer overview of all revisions that took place in the document, // the changes must be accepted/rejected by the revisions themselves, the RevisionCollection, or the document // In this case we will reject all revisions via the collection, reverting the document to its original form, which we will then save revisions.rejectAll(); Assert.assertEquals(revisions.getCount(), 0);
getRevisionType | |
public int getRevisionType() |
Example:
Shows how to check if a document has revisions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normal editing of the document does not count as a revision builder.write("This does not count as a revision. "); Assert.assertFalse(doc.hasRevisions()); // In order for our edits to count as revisions, we need to declare an author and start tracking them doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); // This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually // done there and not the programmatic changes we are about to do here Assert.assertFalse(doc.getTrackRevisions()); // As well as nodes in the document, revisions get referenced in this collection Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(doc.getRevisions().getCount(), 1); Revision revision = doc.getRevisions().get(0); Assert.assertEquals(revision.getAuthor(), "John Doe"); Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. "); Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION); Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date())); Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0)); // Deleting content also counts as a revision // The most recent revisions are put at the start of the collection doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION); Assert.assertEquals(doc.getRevisions().getCount(), 2); // Insert revisions are treated as document text by the GetText() method before they are accepted, // since they are still nodes with text and are in the body Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1."); // Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection doc.getRevisions().get(0).accept(); Assert.assertEquals(doc.getRevisions().getCount(), 1); // Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here Assert.assertEquals(doc.getText().trim(), "This is revision #1."); // The second insertion revision is now at index 0, which we can reject to ignore and discard it doc.getRevisions().get(0).reject(); Assert.assertEquals(doc.getRevisions().getCount(), 0); Assert.assertEquals(doc.getText().trim(), "");
Method Detail |
---|
accept | |
public void accept() throws java.lang.Exception |
Example:
Shows how to check if a document has revisions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normal editing of the document does not count as a revision builder.write("This does not count as a revision. "); Assert.assertFalse(doc.hasRevisions()); // In order for our edits to count as revisions, we need to declare an author and start tracking them doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); // This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually // done there and not the programmatic changes we are about to do here Assert.assertFalse(doc.getTrackRevisions()); // As well as nodes in the document, revisions get referenced in this collection Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(doc.getRevisions().getCount(), 1); Revision revision = doc.getRevisions().get(0); Assert.assertEquals(revision.getAuthor(), "John Doe"); Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. "); Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION); Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date())); Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0)); // Deleting content also counts as a revision // The most recent revisions are put at the start of the collection doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION); Assert.assertEquals(doc.getRevisions().getCount(), 2); // Insert revisions are treated as document text by the GetText() method before they are accepted, // since they are still nodes with text and are in the body Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1."); // Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection doc.getRevisions().get(0).accept(); Assert.assertEquals(doc.getRevisions().getCount(), 1); // Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here Assert.assertEquals(doc.getText().trim(), "This is revision #1."); // The second insertion revision is now at index 0, which we can reject to ignore and discard it doc.getRevisions().get(0).reject(); Assert.assertEquals(doc.getRevisions().getCount(), 0); Assert.assertEquals(doc.getText().trim(), "");
reject | |
public void reject() throws java.lang.Exception |
Example:
Shows how to check if a document has revisions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normal editing of the document does not count as a revision builder.write("This does not count as a revision. "); Assert.assertFalse(doc.hasRevisions()); // In order for our edits to count as revisions, we need to declare an author and start tracking them doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); // This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually // done there and not the programmatic changes we are about to do here Assert.assertFalse(doc.getTrackRevisions()); // As well as nodes in the document, revisions get referenced in this collection Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(doc.getRevisions().getCount(), 1); Revision revision = doc.getRevisions().get(0); Assert.assertEquals(revision.getAuthor(), "John Doe"); Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. "); Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION); Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date())); Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0)); // Deleting content also counts as a revision // The most recent revisions are put at the start of the collection doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION); Assert.assertEquals(doc.getRevisions().getCount(), 2); // Insert revisions are treated as document text by the GetText() method before they are accepted, // since they are still nodes with text and are in the body Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1."); // Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection doc.getRevisions().get(0).accept(); Assert.assertEquals(doc.getRevisions().getCount(), 1); // Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here Assert.assertEquals(doc.getText().trim(), "This is revision #1."); // The second insertion revision is now at index 0, which we can reject to ignore and discard it doc.getRevisions().get(0).reject(); Assert.assertEquals(doc.getRevisions().getCount(), 0); Assert.assertEquals(doc.getText().trim(), "");