【问题标题】:How do I change color of a particular word document using apache poi?如何使用 apache poi 更改特定 word 文档的颜色?
【发布时间】:2019-01-02 22:31:59
【问题描述】:

电流输出:

需要的输出:

上面是.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);
                }
            }
        }
    }

【问题讨论】:

  • r.setColor("DC143C"); 放在 r.setText(origText, 0); 之后对我有用。
  • 这对我不起作用。 @AxelRichter
  • 那么我们需要更多信息。至少一张Word文档的图片。最好在某处上传示例文档。
  • 哦,r.setColor("DC143C") 将改变包含被替换的“a”的整个运行的颜色。这是问题吗?但这与“它不起作用”相去甚远。
  • 已上传对不起,我无法上传原始文档的快照,因为我没有足够的积分。而 setColor("DC143C") 真的不适合我。它根本不会改变文本颜色。 @AxelRichter

标签: java apache-poi xwpf


【解决方案1】:

如果只需要改变一个角色的颜色,那么这个角色必须自己运行。这是因为只有运行可以设置样式。

如果您的文档已经包含文本,那么您必须遍历所有已经存在的运行,并可能将这些运行拆分为多个运行。因此,每个应单独设置样式的字符串部分必须单独运行,即使它只有一个字符。

例子:

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"));

 }
}

【讨论】:

  • 天哪!多谢!从昨天开始,我一直在寻找答案,我对拆分运行有一个模糊的印象,但后来我不知道该怎么做。文档是不够的,或者我可能没有在正确的地方寻找答案。你的例子是我一直在寻找的答案。非常感谢!
  • 在所有答案中,您将文本设置在位置 0,为什么?如果 Run 比这更复杂怎么办?
猜你喜欢
  • 2012-11-10
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多