首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Aspose.Words shape.remove()不完全有效

Aspose.Words shape.remove()不完全有效
EN

Stack Overflow用户
提问于 2018-04-16 15:01:03
回答 1查看 230关注 0票数 0

我想要将图像替换为文本,但当我删除形状节点时,结果是一些图片没有被删除。为什么?

代码语言:javascript
复制
    public void replaceImageToMark() throws Exception {
    Document doc = new Document("D:\\company\\demo.docx");
    NodeCollection shapeCollection = doc.getChildNodes(NodeType.DRAWING_ML, true);
    for (int i = 0; i < shapeCollection.getCount(); i++) {
        DrawingML drawingML = (DrawingML) shapeCollection.get(i);
        if (drawingML.hasImage()) {
            String imageFileName = java.text.MessageFormat.format(
                    "Image.ExportImages.{0} Out{1}", i, FileFormatUtil.imageTypeToExtension(drawingML.getImageData().getImageType()));
            DrawingMLImageData imageData = drawingML.getImageData();
            imageData.save("D:\\company\\pic\\" + imageFileName);
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.moveTo(drawingML);
            builder.write("${image}");
            drawingML.remove(); // There are some images that are not effective
        }
    }
    doc.save("D:\\company\\newDemo.docx");
}
EN

回答 1

Stack Overflow用户

发布于 2018-04-17 00:07:05

您正在使用旧版本的Aspose.Words。我建议您使用最新版本的Aspose.Words for Java 18.4。请将代码中的DrawingML替换为Shape,如下所示,以获得所需的输出。

代码语言:javascript
复制
Document doc = new Document(MyDir + "in.docx");

NodeCollection shapeCollection = doc.getChildNodes(NodeType.SHAPE, true);
for (int i = 0; i < shapeCollection.getCount(); i++) {
    Shape shape = (Shape) shapeCollection.get(i);
    if (shape.hasImage()) {
        String imageFileName = java.text.MessageFormat.format(
                "Image.ExportImages.{0} Out{1}", i, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
        shape.getImageData().save("D:\\company\\pic\\" + imageFileName);
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.moveTo(shape);
        builder.write("${image}");
        shape.remove();  
    }
}
doc.save(MyDir + "output.docx");

我与Aspose一起工作,作为开发人员的布道者。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49851287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档