如何使用 apache poi 更改特定 word 文档的颜色?
How do I change color of a particular word document using apache poi?
当前输出:
需要输出:
上面是.docx的截图,下面是代码示例代码,我想把a
换成@
后改颜色。 r.setColor("DC143C")
无效:
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);
}
}
}
}
如果只需要改变一个字符的颜色,那么这个字符必须在它自己的 运行 中。这是因为只有 运行 可以设置样式。
如果您已经有一个包含文本的文档,那么您必须 运行 遍历所有已经存在的 运行,并可能将这些 运行 拆分为多个。因此,每个应单独设置样式的字符串部分必须在其自己的 运行 中,如果它只有一个字符也是如此。
示例:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import java.awt.Desktop;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
public class WordReadAndWrite {
public static void main(String[] args) throws IOException, InvalidFormatException {
XWPFDocument doc = new XWPFDocument(new FileInputStream("source.docx"));
for (XWPFParagraph p : doc.getParagraphs()) { //go through all paragraphs
int runNumber = 0;
while (runNumber < p.getRuns().size()) { //go through all runs, we cannot use for each since we will possibly insert new runs
XWPFRun r = p.getRuns().get(runNumber);
String runText = r.getText(0);
if (runText != null && runText.contains("a")) { //if we have a run with an "a" in it, then
char[] runChars = runText.toCharArray();
StringBuffer sb = new StringBuffer();
for (int charNumber = 0; charNumber < runChars.length; charNumber++) { //go through all characters in that run
if (runChars[charNumber] == 'a') { //if the charcter is an 'a' then
r.setText(sb.toString(), 0); //set all characters, which are current buffered, as the text of the actual run
r = p.insertNewRun(++runNumber); //insert new run for the '@' as the replacement for the 'a'
r.setText("@", 0);
r.setColor("DC143C");
r = p.insertNewRun(++runNumber); //insert new run for the next characters
sb = new StringBuffer(); //empty buffer
} else {
sb.append(runChars[charNumber]); //buffer all characters which are not 'a's
}
}
r.setText(sb.toString(), 0); //set all characters, which are current buffered, as the text of the actual run
}
runNumber++;
}
}
doc.write(new FileOutputStream("result.docx"));
doc.close();
System.out.println("Done");
Desktop.getDesktop().open(new File("result.docx"));
}
}
当前输出:
需要输出:
上面是.docx的截图,下面是代码示例代码,我想把a
换成@
后改颜色。 r.setColor("DC143C")
无效:
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);
}
}
}
}
如果只需要改变一个字符的颜色,那么这个字符必须在它自己的 运行 中。这是因为只有 运行 可以设置样式。
如果您已经有一个包含文本的文档,那么您必须 运行 遍历所有已经存在的 运行,并可能将这些 运行 拆分为多个。因此,每个应单独设置样式的字符串部分必须在其自己的 运行 中,如果它只有一个字符也是如此。
示例:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import java.awt.Desktop;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
public class WordReadAndWrite {
public static void main(String[] args) throws IOException, InvalidFormatException {
XWPFDocument doc = new XWPFDocument(new FileInputStream("source.docx"));
for (XWPFParagraph p : doc.getParagraphs()) { //go through all paragraphs
int runNumber = 0;
while (runNumber < p.getRuns().size()) { //go through all runs, we cannot use for each since we will possibly insert new runs
XWPFRun r = p.getRuns().get(runNumber);
String runText = r.getText(0);
if (runText != null && runText.contains("a")) { //if we have a run with an "a" in it, then
char[] runChars = runText.toCharArray();
StringBuffer sb = new StringBuffer();
for (int charNumber = 0; charNumber < runChars.length; charNumber++) { //go through all characters in that run
if (runChars[charNumber] == 'a') { //if the charcter is an 'a' then
r.setText(sb.toString(), 0); //set all characters, which are current buffered, as the text of the actual run
r = p.insertNewRun(++runNumber); //insert new run for the '@' as the replacement for the 'a'
r.setText("@", 0);
r.setColor("DC143C");
r = p.insertNewRun(++runNumber); //insert new run for the next characters
sb = new StringBuffer(); //empty buffer
} else {
sb.append(runChars[charNumber]); //buffer all characters which are not 'a's
}
}
r.setText(sb.toString(), 0); //set all characters, which are current buffered, as the text of the actual run
}
runNumber++;
}
}
doc.write(new FileOutputStream("result.docx"));
doc.close();
System.out.println("Done");
Desktop.getDesktop().open(new File("result.docx"));
}
}