com.aspose.words.GroupShape cannot be cast to com.aspose.words.Shape

when convert word to html

java.lang.ClassCastException: com.aspose.words.GroupShape cannot be cast to com.aspose.words.Shape

Thanks

shape.zip (27.6 KB)

@hlgao,

After an initial test with the licensed latest version of Aspose.Words for Java i.e. 19.3, we were unable to reproduce this issue on our end. We would suggest you please upgrade to the latest version of Aspose.Words for Java on your end. Hope, this helps.

We used the following code on our end for testing:

Document doc = new Document("E:\\temp\\shape.docx");
doc.save("E:\\temp\\awjava-19.3.html");

See attached: awjava-19.3.zip (4.4 KB)

@awais.hafeez
Thank you for the code.
I use the latest version of Aspose.Words for Java, but there still a problem here.

Document doc = new Document(“D:/test/shape.docx”);
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);
options.setOfficeMathOutputMode(1);
options.setImageSavingCallback(new HandleImageSaving());
doc.save(“D:/test/shape.html”,options);

class HandleImageSaving implements IImageSavingCallback {
public void imageSaving(ImageSavingArgs args) throws Exception {
Shape shape = ((Shape) args.getCurrentShape());
if (shape.hasImage() && shape.getAlternativeText().equals(“OfficeMath”)) {
args.setImageFileName(args.getImageFileName().replace(".png", “.svg”));
args.setImageStream(new ByteArrayOutputStream());
args.setKeepImageStreamOpen(false);
}

}
}

this code will cause an error to occur

Shape shape = ((Shape) args.getCurrentShape());

@hlgao,

Please use the following code to fix this exception:

static class HandleImageSaving implements IImageSavingCallback {
    public void imageSaving(ImageSavingArgs args) throws Exception {

        if (args.getCurrentShape().getNodeType() == NodeType.GROUP_SHAPE) {
            GroupShape gs = (GroupShape) args.getCurrentShape();
            // todo
        } else {
            Shape shape = ((Shape) args.getCurrentShape());
            if (shape.hasImage() && shape.getAlternativeText().equals("OfficeMath")) {
                args.setImageFileName(args.getImageFileName().replace(".png", ".svg"));
                args.setImageStream(new ByteArrayOutputStream());
                args.setKeepImageStreamOpen(false);
            }
        }
    }
}

@awais.hafeez
It’s working. Thank you very much.