如何使用apache poi更改特定word文档的颜色? [英] How do I change color of a particular word document using apache poi?

查看:137
本文介绍了如何使用apache poi更改特定word文档的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前输出快照

我想要的输出

上面是.docx的快照,下面是代码示例代码,我希望在<$ c $替换之后更改 a 的颜色C> @ 。 r.setColor(DC143C)不起作用:

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.

示例:

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

 }
}

这篇关于如何使用apache poi更改特定word文档的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆