Hello,
having issues with the attached document, when trying to it save as a pdf. This is a big document, having some thousand pages. The issue is that we are unable to cancel the save operation if it takes too much time. The saving operation seems to continue, as well as the data remain in memory. So my questions would be if there is a safe way to cancel saving operations in Aspose.Words? Solutions that have been mentioned in this forum like IDocumentSavingCallback do not seem to work on this one. Any suggestions?
https://mscrmaddons2.blob.core.windows.net/arno/StaticDocTest.docx?sv=2023-01-03&st=2024-09-23T05%3A12%3A05Z&se=2024-09-24T05%3A12%3A05Z&sr=b&sp=r&sig=UffWMFCpkJAlEmXunkvTn95R9kH4yyhft%2FfjnUxHYxs%3D
@cternek IDocumentSavingCallback
can be used 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. On my side it takes about 19 minutes to load the document and build it’s layout. 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.
Hello, tried the above solution and it did work to a certain extend. My question though would still be if there’s any safe method to interupt the doc.Save() process directly. The issue here is that when trying the abort that process, it seems as if the memory is not beeing released.
@cternek The only way to interrupt document save operation is using IDocumentSavingCallback
. Memory should be released once the Document
object is out of scope and garbage collector does it’s work.
Just realized that when using the Callback and aborting the UpdatePageLayout() process and NOT saving the document afterwards, Memory is not freed. Any suggestions on this one? It seems though memory is freed when saving the doc afterwards. Just makes no sense in our case to save a doc that’s not complete.
@cternek Could you please create a simple application that will allow us to preproduce the problem on our side? We will check the issue and provide you more information.
@alexey.noskov sorry, I was meant the IDocumentSavingCallback. This is where this happens with the document I sent you. I just tried the Sample Code you provided. Tried as an html but then later also realized that it does not seem to work at all for pdf. Is it supposed to work for pdf files?
@cternek What do you mean does not work? What exactly does not work? The code I have provided above cancels the document layout operation after building few pages layout so only few first pages are rendered to the output PDF.
@alexey.noskov sorry, maybe you got me wrong, IPageLayoutCallback is working as expected. I refered to the IDocumentSavingCallback and the code examples shown in the article about this functionality. The saving process is not aborted when trying to save it as a pdf, that’s why asked if it is supposed to work for pdf. However tried saving it as html and the cancelation worked, but still the data stayed in memory.
@cternek As I have mentioned most time and resources are consumed by document layout process upon saving to PDF. So IDocumentSavingCallback
might be not applicable for PDF, since it does not cancel the document layout process.