Aspose转换过程中有没有办法控制转换进程?

1)使用Aspose.Slides for Java 和Aspose.Words for Java 将ppt文档或者word文档转换成png图片的时候,是否有相关api可以获取到文档转换的进度或者预估时间?
2)如果转换时间超过了预期时间,是否可以终止掉正在转换的任务?

我想实现的目的:如果我用 Aspose.Slides for Java 19.12 开启10条线程,同时进行把PPT文档转换成Png图片,如果某一个线程在转换过程中超过5分钟还没转换成功,我可以把这一条线程关闭掉。.

1 Like

@zg0x1231.

我已经观察到您所共享的要求,并且很遗憾地分享,目前API中尚无所需的支持。 我们的问题跟踪系统中已创建了ID为SLIDESJAVA-38014的问题,以进一步研究为提供渲染过程中的经过时间提供支持的这一要求。 一旦问题得到解决,我们将与您分享反馈。

@zg0x1231.

关于Aspose.Words的查询,可以使用以下代码示例来满足您的要求。 希望这对您有所帮助。

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.out.println("Thread didn't stop properly");
}

private static Runnable infiniteProcessingLoop()
{
    return new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                while (true)
                {
                    Document doc = new Document("input.docx");
                    doc.save("output.png");
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

        }
    };
}

Aspose.Slides中
com.aspose.slides.Presentation ppt = new Presentation(“test.pptx”);
这一步操作可以终止吗?

@zg0x1231,

我之前的文章中共享的代码与Aspose.Words有关。

The issues you have found earlier (filed as SLIDESJAVA-38014) have been fixed in this update.

aspose.slides 20.6发现此方法并不可行,不能终止该线程

private static Runnable infiniteProcessingLoop() {
    return new Runnable() {
        @Override
        public void run() {
            try {
                while (true) {
                    Presentation document = new Presentation("inputFile.pptx");
                    document.save("1.pdf", SaveFormat.Pdf);
                    System.out.println("加载成功");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    };
}

public static void main(String[] args) {
    Thread infiniteThread = new Thread(infiniteProcessingLoop());
    infiniteThread.start();

    // interrupt it in a while
    try {
        Thread.sleep(1000);
        infiniteThread.interrupt();
        // wait till it dies
        infiniteThread.join(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // check if it is dead
    System.out.println(infiniteThread.getState());
    if (infiniteThread.getState() == Thread.State.RUNNABLE){
        System.out.println("Thread didn't stop properly");
    }
}

@asposeren,

与该线程相关的问题是有关支持获得渲染进度并终止任何线程的支持。 但是,您的问题似乎有所不同。 如果您仍然发现问题,可以请尝试使用建议的代码并与我们分享。

InterruptionTokenSource token = new InterruptionTokenSource();

Runnable interruption = new Runnable() {
    public void run() {
        try {
            java.lang.Thread.sleep(30000); //interruption timer
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        token.interrupt(); //interrupt all action 
    }
};
java.lang.Thread th1 = new java.lang.Thread(interruption);
th1.start();

LoadOptions lo = new LoadOptions();
lo.setInterruptionToken(token.getToken());

Presentation pres = null;
try {
    pres = new Presentation(path, lo);

    //other operations 
} catch (Exception e) {
    if (!token.getToken().isInterruptionRequested())
        e.printStackTrace();
}finally {
    if (pres != null) pres.dispose();
}

aspose.word和cells pdf都支持吗,可以给出相关示例代码吗

@asposeren

请使用以下代码示例Aspose.Words for Java.

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.out.println("Thread didn't stop properly");
}
private static Runnable infiniteProcessingLoop()
{
    return new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                while (true)
                {
                    Document doc = new Document("input.docx");
                    doc.save("output.png");
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

        }
    };
}

关于Aspose.Cells,请阅读以下文章。

Java的Aspose.Cells-可中断库

@asposeren

遗憾的是,Aspose.PDF目前不提供此类功能。为了实现,已将一个功能请求PDFJAVA-39534记录在我们的问题跟踪系统中。我们将进一步调查其可行性,并向您发布其可用性状态。请给我们一点时间。