简体   繁体   English

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

[英]Aspose.Words shape.remove() not fully effective

I want to replace images to text but when I remove the shape nodes,As a result, some of the pictures are not deleted. 我想将图像替换为文本,但是当我删除形状节点时,结果是某些图片没有被删除。 Why? 为什么?

    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");
}

You are using older version of Aspose.Words. 您正在使用旧版本的Aspose.Words。 I suggest you please use latest version of Aspose.Words for Java 18.4 . 我建议您使用Aspose.Words for Java 18.4的最新版本。 Please replace DrawingML with Shape in your code as shown below to get the desired output. 请在代码中将Shape替换为Shape,如下所示,以获取所需的输出。

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");

I work with Aspose as Developer Evangelist. 我与Aspose一起担任开发人员推广人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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