Hello Aspose Team,
I have a specific situation in my Word file:
I need to find revisions which are actually Section Breaks and then accept those revisions programmatically. I am using Java and Aspose.Words. Is there a way to do this directly, or should I use a Visitor or some other approach?
I’ve attached:
- The Word file,
sections.docx (119.3 KB)
- Screenshot showing the active revision (Section Break),
- View of the file in Document Explorer.
Any suggestions or sample code would be appreciated!
Best regards,
Nevena
@NevenaB
Solution
Iterate through the document’s Revision collection, locate the revisions whose affected node is a section‑break (Break with BreakType.SECTION_BREAK), and call accept() on those revisions. All other revisions remain untouched.
Code (Java)
import com.aspose.words.*;
public class AcceptSectionBreakRevisions {
public static void main(String[] args) throws Exception {
// Load the document that contains tracked changes
Document doc = new Document("sections.docx");
// Walk through every revision in the document
for (Revision rev : doc.getRevisions()) {
// We are interested only in insert or delete revisions
RevisionType type = rev.getRevisionType();
if (type != RevisionType.INSERT && type != RevisionType.DELETE)
continue;
// The node that the revision is attached to
Node revNode = rev.getParentNode();
// Section breaks are represented by a Break node
if (revNode instanceof Break) {
Break brk = (Break) revNode;
// Check that the break is a section break
if (brk.getBreakType() == BreakType.SECTION_BREAK) {
// Accept this specific revision
rev.accept();
}
}
}
// Save the result
doc.save("sections_accepted.docx");
}
}
Alternative – Visitor approach
If you prefer a tree walk, subclass NodeVisitor and handle Break nodes directly:
class SectionBreakVisitor extends NodeVisitor {
@Override
public int visitBreak(Break breakNode) {
// Is this break part of a tracked insertion or deletion?
if ((breakNode.isInsertRevision() || breakNode.isDeleteRevision()) &&
breakNode.getBreakType() == BreakType.SECTION_BREAK) {
// The revision that owns this node is the one attached to the break
Revision rev = breakNode.getDocument().getRevisions()
.stream()
.filter(r -> r.getParentNode() == breakNode)
.findFirst()
.orElse(null);
if (rev != null) {
rev.accept();
}
}
return VisitorAction.CONTINUE;
}
}
// Usage
Document doc = new Document("sections.docx");
doc.accept(new SectionBreakVisitor());
doc.save("sections_accepted.docx");
Verification
Open the saved sections_accepted.docx in Microsoft Word. Only the tracked changes that correspond to section‑break insertions/deletions should be accepted; all other revisions remain pending.
Please validate this behaviour with your exact SDK version and a minimal repro, as support can vary by release.
@NevenaB You can try using the following code:
Document doc = new Document("C:\\Temp\\in.docx");
for(Paragraph p : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
if(p.isDeleteRevision() && p.isEndOfSection())
p.getRange().getRevisions().acceptAll();
}
doc.save("C:\\Temp\\out.docx");