Let's take, first <w:r> from the below sample input. In that <w:t> contains Fig 1. Now I need to create new <w:r> for each character in <w:t>. (i.e) F should be in separate <w:r>, i should be in separate <w:r>, g should be in separate <w:r> and so on. Like the below structure.
<w:r><w:t>F</w:t></w:r>
<w:r><w:t>i</w:t></w:r>
<w:r><w:t>g</w:t></w:r>
<w:r><w:t> </w:t></w:r>
<w:r><w:t>1</w:t></w:r>
<w:r><w:t>.</w:t></w:r>
Code Snippet:
if (run.getParent() instanceof ContentAccessor) {
parent = (ContentAccessor) run.getParent();
List<Object> childElements = getAllchildObj(run);
if (childElements.size() > 0) {
for (Object child : childElements) {
int currentRunIndex = parent.getContent().indexOf(run);
if (child.getClass().getName() == "org.docx4j.wml.RPr") {
continue;
}
if (child instanceof Text) {
String txt = ((Text) child).getValue();
List<String> textElementString = textSplit(txt);
for (String textElement : textElementString) {
R newRun = factory.createR();
newRun.setParent(parent);
RPr tempRunProps = run.getRPr();
if (tempRunProps != null) {
String stringRunProps = XmlUtils.marshaltoString(tempRunProps);
if (stringRunProps != null || stringRunProps.equalsIgnoreCase("")) {
newRun.setRPr((RPr) XmlUtils.unmarshalString(stringRunProps));
}
}
Text t = factory.createText();
t.setSpace("preserve");
t.setValue(textElement);
newRun.getContent().add(t);
try {
parent.getContent().add(currentRunIndex, newRun);
} catch (IndexOutOfBoundsException e) {
logger.error("PM-324e55dc-b928-4c8f-9ecc-bce47b8cb54b: IndexOutOfBoundsException"+e.getMessage());
}
}
}else {
R newRun = factory.createR();
newRun.setParent(parent);
currentRunIndex = parent.getContent().indexOf(run);
RPr runProps = factory.createRPr();
RPr tempRunProps = run.getRPr();
if (tempRunProps != null) {
String stringRunProps = XmlUtils.marshaltoString(tempRunProps);
if (stringRunProps != null || stringRunProps.equalsIgnoreCase(""){
newRun.setRPr((RPr) XmlUtils.unmarshalString(stringRunProps));
}
}
newRun.getContent().add(child);
parent.getContent().add(currentRunIndex, newRun);
}
}
}
parent.getContent().remove(run);
}
The above snippet is working fine. If the run.getParent() is instance of ContentAccessor. Here for the below input, I'm not getting the expected output because <w:r> present inside <w:moveTo> (RunTrackChange). How I need to process, if run.getParent() is instance of RunTrackChange.
Sample input:
<w:p>
<w:pPr>
<w:pPrChange w:author="Judi Nath" w:date="2021-02-26T11:39:00Z" w:id="18">
<w:pPr>
<w:spacing w:line="360" w:lineRule="auto" />
<w:ind w:left="-480" />
</w:pPr>
</w:pPrChange>
</w:pPr>
<w:bookmarkStart w:name="para10020" w:id="10020" />
<w:moveToRangeStart w:author="Judi Nath" w:date="2021-02-26T11:39:00Z" w:name="move65231970" w:id="19" />
<w:proofErr w:type="gramStart" />
<w:moveTo w:author="Judi Nath" w:date="2021-02-26T11:39:00Z" w:id="20">
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b/>
<w:sz w:val="24" />
</w:rPr>
<w:t>Fig 1.</w:t>
</w:r>
<w:proofErr w:type="gramEnd" />
<w:del w:author="Judi Nath" w:date="2021-02-26T11:39:00Z" w:id="23">
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:sz w:val="24" />
</w:rPr>
<w:delText xml:space="preserve"> A</w:delText>
</w:r>
</w:moveTo>
<w:bookmarkEnd w:id="10020" />
</w:p>
Expected Output:
<w:p>
<w:pPr>
<w:pPrChange w:author="Judi Nath" w:date="2021-02-26T11:39:00Z" w:id="18">
<w:pPr>
<w:spacing w:line="360" w:lineRule="auto" />
<w:ind w:left="-480" />
</w:pPr>
</w:pPrChange>
</w:pPr>
<w:bookmarkStart w:name="para10020" w:id="10020" />
<w:moveToRangeStart w:author="Judi Nath" w:date="2021-02-26T11:39:00Z" w:name="move65231970" w:id="19" />
<w:proofErr w:type="gramStart" />
<w:moveTo w:author="Judi Nath" w:date="2021-02-26T11:39:00Z" w:id="20">
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b/>
<w:sz w:val="24" />
</w:rPr>
<w:t>F</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b/>
<w:sz w:val="24" />
</w:rPr>
<w:t>i</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b/>
<w:sz w:val="24" />
</w:rPr>
<w:t>g</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b/>
<w:sz w:val="24" />
</w:rPr>
<w:t> </w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b/>
<w:sz w:val="24" />
</w:rPr>
<w:t>1</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b/>
<w:sz w:val="24" />
</w:rPr>
<w:t>.</w:t>
</w:r>
<w:proofErr w:type="gramEnd" />
<w:del w:author="Judi Nath" w:date="2021-02-26T11:39:00Z" w:id="23">
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:sz w:val="24" />
</w:rPr>
<w:delText xml:space="preserve"> </w:delText>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:sz w:val="24" />
</w:rPr>
<w:delText xml:space="preserve">A</w:delText>
</w:r>
</w:moveTo>
<w:bookmarkEnd w:id="10020" />
</w:p>