Shape to Image Conversion Hangs using Java | ImageSaveOptions | Shape.getShapeRenderer

Hi, Team.

Sometimes shapes rendering causes the process is executed indefinitely. Here is a document with shape: example.zip (275.0 KB). The version of Aspose.Word is 21.4.0

The java code:

void saveImageAsPNG(Shape shape, String fileName) {
    ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
    shape.getShapeRenderer().save(fileName, options);
}

@skorpusova

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSJAVA-2568. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Is it possible to add the interruption flag check? If any process in Aspose.Word is executed indefinitely, we cannot interrupt it. Sometimes it happens during image rendering or getting of mathML HTML string. Unfortunately, Java does not allow to stop thread safely, only interrupt. It will be greater if Aspose throws InterruptedException if a process is stopped.

Best regards,
Svetlana

@skorpusova

Unfortunately, there is no API to handle such indefinitely process. However, we have logged your requirement as WORDSNET-22150 in our issue tracking system. We will inform you via this forum thread once there is an update available on it.

@tahir.manzoor,

We do not ask you to use any API here. Just check the interruption flag and throw InterruptedException if the flag is true in any place where infinite loop, recursion, or long cycle are possible:

if (Thread.currentThread().isInterrupted()) {
   throw new InterruptedException();
}

It will help us a lot.

Thank you in advance,
Svetlana

@skorpusova

You may be able to manually interrupt Aspose.Words during long running tasks. You can use following code example to achieve your requirement. Hope this helps you.

public void testThreadInterruption() throws InterruptedException
{
    // run Aspose.Words long operation in a separate thread
    Thread infiniteThread = new Thread(infiniteProcessingLoop());
    infiniteThread.start();

    // interrupt it in a while
    Thread.sleep(2000);
    infiniteThread.interrupt();

    // wait till it dies
    infiniteThread.join(5000);

    // check if it is dead
    if (infiniteThread.getState() == Thread.State.RUNNABLE)
        System.err.println("Thread didn't stop properly");
}

private static Runnable infiniteProcessingLoop()
{
    return new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                while (true)
                {
                    Document doc = new Document(MyDir + "example.docx");
                    Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);

                    ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
                    shape.getShapeRenderer().save(MyDir + "out.png", options);
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

        }
    };
}

Thank you for your example, but it will not work. The thread will be kept because it will work inside shape.getShapeRenderer().save() method. The method will be interrupted only if save method will check isInterrupted flag.

@skorpusova

We will inform you via this forum thread once there is an update available on logged feature WORDSNET-22150.

The issues you have found earlier (filed as WORDSJAVA-2568) have been fixed in this Aspose.Words for Java 21.10 update.

Thank you a lot, Team. I have checked a new version. There is no infinitive loop any more.