Hi
Pls disregard the debug you saw.
If you have a look at what Word does in making stuff bold, you'll see:
- Code: Select all
<!-- Word 2007 emits this -->
<w:p>
<w:pPr>
<w:rPr>
<w:b />
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:b />
</w:rPr>
<w:t>Bold, done by Word - at both pPr and w:r level</w:t>
</w:r>
</w:p>
<!-- Bold, just within the w:r works-->
<w:p>
<w:r>
<w:rPr>
<w:b />
</w:rPr>
<w:t>Bold, just at w:r level</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:rPr>
<w:b w:val="true" />
</w:rPr>
<w:t>Bold, just at w:r level, with val="true"</w:t>
</w:r>
</w:p>
<!-- Bold, just at the w:pPr doesn't work -->
<w:p>
<w:pPr>
<w:rPr>
<w:b w:val="true" />
</w:rPr>
</w:pPr>
<w:r>
<w:t>just pPr/rPr - doesn't work</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:rPr>
<w:b />
</w:rPr>
</w:pPr>
<w:r>
<w:t>just pPr/rPr - doesn't work</w:t>
</w:r>
</w:p>
So you can see that you must have w:b in the w:r, and can optionally have it in the w:pPr.
If you use the createParagraphOfText method, you still need to get the w:r in order to set w:b in it.
So its easier not to do that.
The long winded way to do it:
- Code: Select all
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.Text t = factory.createText();
t.setValue("text");
org.docx4j.wml.R run = factory.createR();
run.getRunContent().add(t);
p.getParagraphContent().add(run);
org.docx4j.wml.RPr rpr = factory.createRPr();
org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
b.setVal(true);
rpr.setB(b);
run.setRPr(rpr);
// Optionally, set pPr/rPr@w:b
org.docx4j.wml.PPr ppr = factory.createPPr();
p.setPPr( ppr );
org.docx4j.wml.ParaRPr paraRpr = factory.createParaRPr();
ppr.setRPr(paraRpr);
rpr.setB(b);
// Add it to the doc
wordMLPackage.getMainDocumentPart().addObject(p);
Here is an easier way:
- Code: Select all
String str = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:rPr><w:b /></w:rPr><w:t>Bold, just at w:r level</w:t></w:r></w:p>";
wordMLPackage.getMainDocumentPart().addObject(
org.docx4j.XmlUtils.unmarshalString(str) );
fyi, XmlUtils also contains a method unmarshallFromTemplate, which you can use if you need to replace things in the string.
cheers
Jason