I have dotnet core application which uses many aspose libraries to convert different document types to pdf, if it is taking too long, how can i cancel the operation.
I came accross IPageLayoutCallback for aspose.words, are there similar interfaces for other libs too
@Naqib You can use IDocumentSavingCallback to cancel save operation. The example in the documentation demonstrates how to cancel save operation if it takes more time than expected.
Upon saving document to PDF however the most time is taken by document layout process than by saving operation. You can interrupt the layout process using IPageLayoutCallback. For example the following code interrupts the layout process when layout of two first pages is built:
int pagesToRender = 2;
Document doc = new Document("C:\\Temp\\in.docx");
doc.LayoutOptions.Callback = new PageLayoutCallback(pagesToRender);
try
{
doc.UpdatePageLayout();
}
catch (PageLimitReachedException ex)
{
PdfSaveOptions opt = new PdfSaveOptions();
opt.PageSet = new PageSet(new PageRange(0, pagesToRender - 1));
doc.Save("C:\\Temp\\out.pdf", opt);
}
private class PageLayoutCallback : IPageLayoutCallback
{
public PageLayoutCallback(int maxPageCount)
{
mMaxPageCount = maxPageCount;
}
public void Notify(PageLayoutCallbackArgs args)
{
if (args.Event == PageLayoutEvent.PartReflowStarted &&
args.PageIndex >= mMaxPageCount)
throw new PageLimitReachedException();
}
private int mMaxPageCount;
}
private class PageLimitReachedException : Exception { }
In the Notify method you can use other condition for the layout process interruption. For example check the flag set by the outside event.