简体   繁体   中英

How to get italic style using docx4j in java?

I want to get applied italic styles on word file data using docx4j api. but i am not getting it, anyone can help me please.

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

Below is my Input docx file text....

REFERENCES
Curry, RA, & Tempkin, BB (2010). Sonography: Introduction to normal structure and functional anatomy (3rd ed.). St. Louis: Saunders.
Frank, E., & Long, B. (2011). Merrill's atlas of positioning and radiographic procedures (12th ed.). St. Louis: Mosby.

In this file i have Maintitle character style that contains italic styles, italic not applied directly....

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();
    }
The actual solution is:-

Style tStyle = (Style)rPr.getParent();
if( tStyle.getRPr().getI().isVal()){
   isItalic = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM