above are the snapshots of the .docx and below is the code sample code, I want to change the color of a
after it is replaced by @
. r.setColor("DC143C")
doesn't work:
for (XWPFParagraph p : docx.getParagraphs()) {
List<XWPFRun> runs = p.getRuns();
if (runs != null) {
for (XWPFRun r : runs) {
String origText = r.getText(0);
if (origText != null && origText.contains("a")) {
origText = origText.replace("a", "@");
r.setText(origText, 0);
}
}
}
}
If the need is to change the color of just one character then this character must be in its own run. This is because only runs can be styled.
If you have a document containing text already then you must run through all already existing runs and possible split those runs into multiple ones. As the result each string part which shall be styled separately must be in its own run, also if it is only one character.
Example: