I’m playing with apache poi, trying to convert a list of text diffs into a word document that includes those diffs as tracked changes.
Using the answer from
Apache poi: insert text in a paragraph and view track changes in a word document
Insertions have been easy enough. Adapting this to deletions has proved trickier than I’d hoped.
The following results in documents that word reports having unreadable content (and does not create deletions in track changes).
static XWPFRun createDelRun(XWPFParagraph paragraph, String author, java.util.Calendar date) {
CTRunTrackChange trackChange = paragraph.getCTP().addNewDel();
trackChange.setAuthor(author);
trackChange.setDate(date);
var ctr = trackChange.addNewR();
return new XWPFRun(ctr, paragraph);
}
I’m unfamiliar with both apache-poi and the docx schema, so realise this approach may be fundamentally wrong.
How should I be doing this?
After examing the xml from some example word docs the issue became clear.
Where as insertions can be performed by calling text methods etc on an XWPFRun, deletion details are stored under an “r” CTR object.
The following works.
public void text(XWPFParagraph p, String text) {
CTRunTrackChange trackChange = p.getCTP().addNewDel();
trackChange.setAuthor("author");
trackChange.setDate(new GregorianCalendar());
var ctr = trackChange.addNewR();
var delText = ctr.addNewDelText();
delText.setStringValue(text);
}