How set bold for docx run apache poi

2019-08-22 10:52发布

问题:

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

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

回答1:

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