So the xml looks like this:
- Code: Select all
<w:p w:rsidR="000060E0" w:rsidRDefault="00EF2C0C" w:rsidP="000060E0">
<w:r>
<w:fldChar w:fldCharType="begin" />
</w:r>
<w:r>
<w:instrText xml:space="preserve"> MERGEFIELD D6_15_0#Blank_Field</w:instrText>
</w:r>
<w:r>
<w:instrText xml:space="preserve"> \* MERGEFORMAT </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="separate" />
</w:r>
<w:r w:rsidR="000060E0">
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>«D6_15_0#Blank_Field»</w:t>
</w:r>
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<w:fldChar w:fldCharType="end" />
</w:r>
</w:p>
I would like to be able to combine the instructions into one (which is what they should be) and I have tried
- Code: Select all
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
ComplexFieldLocator fl = new ComplexFieldLocator();
new TraversalUtil(documentPart.getContent(), fl);
List<FieldRef> fieldRefs = new ArrayList<>();
for (P p : fl.getStarts()) {
FieldsPreprocessor.canonicalise(p, fieldRefs);
}
for (FieldRef fr : fieldRefs) {
System.out.println(fr.getInstructions().size());
if( fr.getInstructions().size()>1)
{
for( int i=1; i<fr.getInstructions().size(); i++) {
final Object x = fr.getInstructions().get(i);
final Object o = XmlUtils.unwrap(x);
// System.out.println(x);
// System.out.println(o);
if (o instanceof Text) {
final String value = ((Text)o).getValue();
if( StringUtils.isNotEmpty(value)) {
final Object x0 = fr.getInstructions().get(0);
final Object o0 = XmlUtils.unwrap(x0);
if (o0 instanceof Text) {
final String value0 = ((Text)o0).getValue();
((Text)o0).setValue(value0+value);
}
}
}
}
for( int i=fr.getInstructions().size()-1; i>0; i--) {
fr.getInstructions().remove(i);
}
}
}
Which does remove the second instruction but does not write that back to the wordMLPackage. Is there any way to do it, or a better way to do it. Thank you.