Link to home
Start Free TrialLog in
Avatar of Sarfraz Malik
Sarfraz Malik

asked on

How to get italic text using docx4j in java?

I want to get the text upon which italic style is applied in word file, but i am not getting it, anyone can help me please.

rPr.getI() and rPr.getICs() not working for it...

Attached is the sample input file...
Below is my Java Code to read docx file, Code using docx4j api....

try {
        File docxFile = new File("/Users/gwmc-062/Desktop/temp/test.docx");
        WordprocessingMLPackage wProcessorPackage = WordprocessingMLPackage.load(docxFile);
        MainDocumentPart documentPart = wProcessorPackage.getMainDocumentPart();

        Document wmlDocumentEl = (Document) documentPart.getJaxbElement();
        Body body = wmlDocumentEl.getBody();

        List<Object> paraList = TraversalUtil.getChildrenImpl(body);
        for (Object paraObj : paraList) {
            String paraStyle = null;
            boolean paraStyleFlag = false;
            if (paraObj.getClass().getName().equalsIgnoreCase("org.docx4j.wml.P")) {
                String paraText = XmlUtils.unwrap(paraObj).toString();
                PPr pPr = ((P) XmlUtils.unwrap(paraObj)).getPPr();
                if (pPr != null && pPr.getPStyle() != null) {
                    paraStyle = pPr.getPStyle().getVal();
                    paraStyleFlag = true;
                }
                List<Object> paraWRList = TraversalUtil.getChildrenImpl(paraObj);
                for (Object paraWR : paraWRList) {
                    String charStyle = "";
                    boolean charStyleFlag = false, isItalic = false, isBold = false;
                    if (paraWR.getClass().getName().equalsIgnoreCase("org.docx4j.wml.R")) {
                        RPr rPr = ((R) XmlUtils.unwrap(paraWR)).getRPr();
                        if (rPr != null && rPr.getRStyle() != null) {
                            charStyle = rPr.getRStyle().getVal();

                            if (charStyle.toLowerCase().contains("italic"))
                                isItalic = true;
                        }
                        if(rPr != null && (rPr.getI() != null || rPr.getICs() != null)){
                            isItalic = true;
                        }
                        if(rPr != null && (rPr.getB() != null || rPr.getBCs() != null)){
                            isBold = true;
                        }
                        List<Object> textList = TraversalUtil.getChildrenImpl(paraWR);
                        for (Object tObj : textList) {
                            Object textObject = XmlUtils.unwrap(tObj);
                            String className = textObject.getClass().getName();
                            if (className.equalsIgnoreCase("org.docx4j.wml.Text")) {
                                String textContent = ((Text) textObject).getValue();
                            }
                        }
                    }
                }//END paraWR LOOP...
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
test.docx
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Hi!

According to the CheetSheet there is a class called StyleDefinitionsPart.
Also have you run the document through the webapp ?

Regards,
     Tomas Helgi
Avatar of Sarfraz Malik
Sarfraz Malik

ASKER

Thanks Tomas to give your precious time for this problem...
The actual solution is:-

Style tStyle = (Style)rPr.getParent();
if( tStyle.getRPr().getI().isVal()){
   isItalic = true;
}
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.