【问题标题】:Aspose.Words shape.remove() not fully effectiveAspose.Words shape.remove() 不完全有效
【发布时间】:2018-04-16 07:01:02
【问题描述】:

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

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

【问题讨论】:

    标签: java aspose.words


    【解决方案1】:

    您使用的是旧版本的 Aspose.Words。我建议您使用最新版本的Aspose.Words for Java 18.4。请在您的代码中将 DrawingML 替换为 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");
    

    我与 Aspose 合作,担任开发人员宣传员。

    【讨论】:

      猜你喜欢
      • 2022-12-28
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多