如何从Java中的docx文件读取形状和图表?

时间:2019-07-18 04:50:46

标签: java apache-poi docx4j

这是我尝试执行的代码,但未从Docx文件中找到形状。它提供了所有图像的计数,包括形状。 我也尝试过apache-poi,但是没有找到任何解决方案来从doc文件中读取形状。

public class imageExtract {
    Pict pict;
    protected static Logger log = LoggerFactory.getLogger(imageExtract.class);
    public static void main(String[] args) throws Docx4JException, JAXBException {
                File doc = new File("C:\\Users\\Saurabh Srivastav\\Music\\NewGen\\docs\\test22.docx");
                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);

                MainDocumentPart mainDocument = wordMLPackage.getMainDocumentPart();
                List<Object> textList = getAllElementFromObject(mainDocument, Pict.class);
                System.out.println(textList.size());
                for (Object obj : textList) {
                    System.out.println(obj.getClass().getName());
                    Pict rr = (Pict)obj;
                     CTShapeProperties spPr = (CTShapeProperties)obj;
                        STShapeType shapeType = spPr.getPrstGeom().getPrst();
                        System.out.println("Shape of picture" + shapeType.value() );    
                    }
    }


    public static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
        List<Object> result = new ArrayList<Object>();
        if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue();

        if (obj.getClass().equals(toSearch))
            result.add(obj);
        else if (obj instanceof ContentAccessor) {
            List<?> children = ((ContentAccessor) obj).getContent();
            for (Object child : children) {

                result.addAll(getAllElementFromObject(child, toSearch));
            }
        }
        return result;
}

}

0 个答案:

没有答案