Insert Tags in Front of Deletion & Insertion Revisions (Track change) in Word Document using Java

Dear Team,
I tried to get a trackchages for the paragraph, but it returns the empty value,
I use this code (Aspose sample GitHub code):

ParagraphCollection paragraphs = doc.getFirstSection().getBody().getParagraphs();
for (int i = 0; i < paragraphs.getCount(); i++) {
	System.out.println(paragraphs.get(i).toString(SaveFormat.TEXT));
	if (paragraphs.get(i).isMoveFromRevision())
		System.out.println("The paragraph " + i + " has been moved (deleted).");
	if (paragraphs.get(i).isMoveToRevision())
		System.out.println("The paragraph " + i + " has been moved (inserted).");
}

I attached the sample for your reference.
Despatched_Sample.docx (27.4 KB)

If I use revisiongroup or revision class, how i get the parent paragraph, please provide some solution.

Thanks
Rjnick

@rjnick,

You can use Revision.ParentNode property to access the Paragraph. Please check the following code of Aspose.Words for Java.

Document doc = new Document("C:\\Temp\\Despatched_Sample.docx");

for (Revision revision : doc.getRevisions()) {
    if (revision.getParentNode().getNodeType() == NodeType.RUN) {
        Paragraph para = (Paragraph) revision.getParentNode().getAncestor(NodeType.PARAGRAPH);
        if (para != null) {
            int MAX_CHARS = 16;
            String paraText = para.getText();
            if (paraText.length() > MAX_CHARS)
                paraText = paraText.substring(0, MAX_CHARS) + "...";

            System.out.println(paraText + " <-- This Paragraph contains a Node with " +
                    RevisionType.getName(revision.getRevisionType()) + " revision.");
        }
    }
}
1 Like

Thanks, this is working fine for me, please provide solution for finding the position of the insert and delete of track changes.

@rjnick,

Can you please elaborate a bit more, what do you mean when saying “finding the position of the insert and delete of track changes”? Do you want to get the (x, y) coordinates of some content in Word document? Please elaborate with the help of screenshots and provide complete details of your use-case.

@awais.hafeez Thanks for the response,
I attached the screenshot for your ref.
Screenshot 1: Track changes enabled view on MS Word
Screenshot 2 : needed output format(After edit and before edit).
output.PNG (25.5 KB)
Paragrapgh for Trachchanges.PNG (63.5 KB)

@rjnick,

Text in a Word document is represented by Run nodes and you can determine which Run nodes were deleted or inserted by using the following Java code:

Document doc = new Document("C:\\Temp\\Despatched_Sample.docx");

for (Run run : (Iterable<Run>) doc.getChildNodes(NodeType.RUN, true)) {
    if (run.isDeleteRevision())
        System.out.println(run.getText() + " <-- was DETELED");
    else if (run.isInsertRevision())
        System.out.println(run.getText() + " <-- is INSERTED");
}

It gives output of track changes letter I want before track changes para as a one string and after track changes para as another string
in after trackchanges para, need to insert tag if deleted means << DELETED >> and inserted means << INSERTED >>, please refer previous thread screen shot

@rjnick,

We are checking this scenario and will get back to you soon.

1 Like

@awais.hafeez sure, thanks

@awais.hafeez, is there any possible way to achieve this ?

@rjnick,

Please check if the following Java code produces desired output on your end?

Document doc = new Document("C:\\Temp\\Despatched_Sample.docx");
doc.setTrackRevisions(false);

for (Run run : (Iterable<Run>) doc.getChildNodes(NodeType.RUN, true)) {
    if (run.isDeleteRevision())
        run.getParentNode().insertAfter(getRun(doc, "<<DELETED>>"), run);
    else if (run.isInsertRevision())
        run.getParentNode().insertAfter(getRun(doc, "<<INSERTED>>"), run);
}

doc.acceptAllRevisions();
doc.save("C:\\Temp\\awjava-21.5.docx");

public static Run getRun(Document doc, String text) {
    Run run = new Run(doc);
    run.getFont().setHighlightColor(Color.YELLOW);
    run.setText(text);
    return run;
}