阅读形状(矩形,正方形,圆形,箭头等),使用java从MS Word文件剪贴画

时间:2016-06-26 12:53:57

标签: java apache-poi shape aspose

我可以从ms word table获取图像,但无法获得形状和剪贴画。

public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    try {
        String dataDir = "E://test//demo.docx";
        generatePicturesAsImages(dataDir);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void generatePicturesAsImages(String sourcePath) {
    try {
        Document doc = new Document(sourcePath);
        ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
        options.setJpegQuality(100);
        options.setResolution(100);
        // options.setUseHighQualityRendering(true);
        List<ShapeRenderer> pictures = getAllPictures(doc);
        if (pictures != null) {
            for (int i = 0; i < pictures.size(); i++) {
                ShapeRenderer picture = pictures.get(i);
                String imageFilePath = sourcePath + "_output_" + i + ".jpeg";
                picture.save(imageFilePath, options);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static List<ShapeRenderer> getAllPictures(final Document document) throws Exception {
    List<ShapeRenderer> images = null;
    @SuppressWarnings("unchecked")
    NodeCollection<DrawingML> nodeCollection = document.getChildNodes(NodeType.DRAWING_ML, Boolean.TRUE);
    if (nodeCollection.getCount() > 0) {
        images = new ArrayList<ShapeRenderer>();
        for (DrawingML drawingML : nodeCollection) {
            images.add(drawingML.getShapeRenderer());
        }
    }
    return images;
}

以上程序是从表中获取图像所以我应该添加更多来获取形状..请建议我任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您使用的是旧版Aspose.Words。如果要使用旧版本的Aspose.Words,请在getAllPictures方法中使用Document.getChildNodes获取Shape和DrawingML节点的集合。

NodeCollection<DrawingML> drwingmlnodes = document.getChildNodes(NodeType.DRAWING_ML, Boolean.TRUE);

NodeCollection<Shape> shapenodes = document.getChildNodes(NodeType.SHAPE, Boolean.TRUE);

请注意,我们删除了DrawingML from our APIs in Aspose.Words 15.2.0。如果您想使用最新版本的Aspose.Words v16.5.0,请仅使用NodeType.SHAPE。

我与Aspose一起担任开发人员传播者。