如何为docx设置粗体运行Apache POI

问题描述:

如何设置run.getCTR().getRPr()的粗体显示? 我写了这段代码,但是根本不起作用.

How set bold for run with run.getCTR().getRPr()? I write this code but it doesn't work at all.

run.setBold(true);

我在字体大小上也遇到了同样的问题,但是我用以下代码修复了它:

I had the same problem with font-size but I fixed it with this code:

CTHpsMeasure size = CTHpsMeasure.Factory.newInstance();
sizeFa.setVal(new BigInteger((sizePoint * 2) + ""));
run.getCTR().getRPr().setSz(size);
run.getCTR().getRPr().setSzCs(size);

现在,我想使用getCTR()将上述代码设置为粗体.我该怎么办? 谢谢.

Now I want to set bold with code like above, with getCTR(). what should I do? Thanks.

如果需要run.getCTR().getRPr().setSzCs(size);来设置字体大小,则您正在使用Complex Script(Cs)字符.可能是特殊的双向(从右到左)语言(例如阿拉伯语)的字符.

If run.getCTR().getRPr().setSzCs(size); is needed for you to set the font size, then you are using Complex Script (Cs) characters. That might be characters of special bidirectional (right to left) language (arabic for example).

因此,对于粗体,您应该尝试使用

So for bold you should try using CTRPr.setBCs.

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
...
  run.setBold(true);
  CTOnOff ctonoff = CTOnOff.Factory.newInstance();
  ctonoff.setVal(STOnOff.ON);
  run.getCTR().getRPr().setBCs(ctonoff);
...