How to interrupt the task of converting word file to pdf

How to interrupt the task of converting word file to pdf.
We want to control the duration of the conversion task.

@Lycheng Actually layout is the most time consuming operation upon conversion document to fixed page format, such as PDF, or printing. There is IPageLayoutCallback which is called when page layout is built. So I this callback will allow you to achieve what you need.

When will the callback be called, and what value do I return to interrupt the transition?

@Lycheng, 我将您之前的消息移至该主题,因为它是重复的

@Lycheng You can use code like this to interrupt building document layout. The code demonstrates how to force interruption once layout of specified pages is build:

int pagesToRender = 2;
Document doc = new Document("C:\\Temp\\in.doc");
doc.getLayoutOptions().setCallback(new PageLayoutCallback(pagesToRender));
try
{
    doc.updatePageLayout();
}
catch (PageLimitReachedException ex)
{
    PdfSaveOptions opt = new PdfSaveOptions();
    opt.setPageSet(new PageSet(new PageRange(0, pagesToRender - 1)));
    doc.save("C:\\Temp\\out.pdf", opt);
}
private static class PageLayoutCallback implements IPageLayoutCallback
{
    public PageLayoutCallback(int maxPageCount)
    {
        mMaxPageCount = maxPageCount;
    }
 
    @Override
    public void notify(PageLayoutCallbackArgs args) throws Exception {
        if (args.getEvent() == PageLayoutEvent.PART_REFLOW_STARTED &&
                args.getPageIndex() >= mMaxPageCount)
            throw new PageLimitReachedException();
    }
    
    private int mMaxPageCount;
}
private static class PageLimitReachedException extends Exception {
}

Also, you can use the code provided in this thread to interrupt save operation:
https://forum.aspose.com/t/interrupt-the-save-operations-java/209170

ok!thanks!Interrupt conversion by throwing an exception

1 Like