Search code examples
javadocxdocx4j

Italic (Emphasis) paragraph in docx4j


I am creating a document from template using Docx4j.

I wasn't able to find a way how to create a paragraph which would be in Italic (<em> in HTML).

Here is an example of method creating Paragraph with an option to do it in Bold.

private static org.docx4j.wml.P createParagraph(String paragraphContent, boolean addNewLine, boolean bold) {
    org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
    org.docx4j.wml.P p = factory.createP();

    org.docx4j.wml.R run = factory.createR();
    p.getContent().add(run);

    org.docx4j.wml.Text text = factory.createText();
    text.setValue(paragraphContent);
    run.getContent().add(text);

    if(bold){
        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);
    }

    if (addNewLine) {
        run.getContent().add(factory.createBr());
    }

    return p;
}

Does anybody know how make an Italic paragraph?


Solution

  • There is a

    rpr.setI(b);
    

    method. Should work like setB.