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).");
}
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)
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
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;
}