Slow conversion and file is written fully to the memory before conversion

I’m trying to convert word documents using aspose.words.
I’m using it for my web application so users will be able to view the doc files they uploaded.
Problem is that if the file is a bit big (3MB with 500 pages) the conversion time is too slow.
Plus, i see also that the whole file is written to the memory. I’m using a stream because i don’t won’t to be written to the memory. I want the conversion to be done ‘on the fly’. This is code:

public void convert(InputStream stream, OutputStream dstStream) throws Exception {
    Document doc = new Document(stream);
    doc.save(dstStream, com.aspose.words.SaveFormat.PDF);
}

I want the conversion to start before the whole file is written to the memory so i’ll be able start displaying output for the user.
Is there a solution for that?
i have attached an example of 2MB file with conversion time of ~30 seconds. My goal is that the user will see the at list the first page/s immediately and that not the whole file will be wrote to the memory.

Hi Gil,

Thanks for your inquiry. It is
quite difficult to answer such questions because performance and memory
usage all depend on complexity and size of the documents you are
generating. While rendering a document to fixed page formats (e.g. PDF), Aspose.Words needs to build two model in the memory – one for document and the other for rendered document.

Please note that the process of building layout model is
not linear; it may take a minute to render one page and may take a few
seconds to render 100 pages. Also, Aspose.Words has to create APS (Aspose Page Specification)
model in memory and this may again eat some more time for some
documents. Rest assured, we’re always working on improving performance;
but, rendering will be always running slower than simple saving to flow
formats.
Gilomer1:

I want the conversion to start before the whole file is written to the memory so i’ll be able start displaying output for the user. Is there a solution for that?

In your case, I suggest you please save the first page of document to Pdf using following code example and show to user. Once complete Pdf is saved, you can show the whole Pdf to user.

Document doc = new Document(MyDir + "Rendering.doc");
using (Stream stream = File.Create(MyDir + "Rendering.SaveToPdfStreamOnePage Out.pdf"))
{
    PdfSaveOptions options = new PdfSaveOptions();
    options.PageIndex = 0;
    options.PageCount = 1;
    doc.Save(stream, options);
}

Thanks for the respond.
I have tried to do what you suggested: first returning just the first page by changing my code:

public void convert(InputStream stream, OutputStream dstStream) throws Exception {
    Document doc = new Document(stream);
    PdfSaveOptions options = new PdfSaveOptions();
    options.setPageCount(1); 
    doc.save(dstStream, options);
}

I did the test with the attached file (185 KB) for the whole file it took 28-29 seconds. For just the first page it took 21 seconds, this is still too long for user to wait to get a preview for his file. Previewing the first page/s must be immediate (maximum 2 seconds).

I need a better solution to display faster the first page/s.
I added limitations to preview only if the file size is less than x MB and y pages, but for some reasons the function:
doc.getPageCount();
Also takes very long, so i cannot limit conversion by amount of pages either (or that there is a faster way to determine the amount of pages?)

Are there any other solutions for fast display for the first page/s or at list detecting that the conversion is going to take too long so i’ll be able to print to the user a relevant message so he/she won’t wait?

Hi Gil,

Please accept my apologies for late response.

Thanks for your inquiry. I am working over your query and will update you asap.

Take your time.
I just need to get a high quality solution for my problem:

  1. Previewing fast the first pages
  2. OR fast detection that the rendering is going to take too long so i’ll be able to print a relevant message instead of rendering.

Hi Gil,

Thanks for your patience.

Document.PageCount invokes page layout which builds the document in memory so note that with large documents this property can take time. After invoking this property, any rendering operation e.g rendering to PDF or image will be instantaneous.

I have tested the scenario and have managed to reproduce performance issue at my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-10368. We will update you via this forum thread once there is any update available on this issue.

Unfortunately, there is no way to know how much time you should wait
before the conversion ends. We will consider adding a Callback event
that notifies you the progress of conversion. I linked your request to
the appropriate issue (WORDSNET-7187) in our issue tracking
system and you will be notified as soon as this feature is
supported.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-10368) have been fixed in this Aspose.Words for .NET 19.5 update and this Aspose.Words for Java 19.5 update.