简体   繁体   中英

Aspose.Words save to PDF first time is slow

Is there any way to decrease the execution time of the first run of Document.Save method with Aspose.Words?

Consider:

for (var i = 0; i < 5; i++)
    Run();

static void Run()
{
    var sw = new Stopwatch();
    sw.Start();

    var doc = new Document();
    //doc.UpdatePageLayout(); // Makes no difference
    var memoryStream = new MemoryStream();
    doc.Save(memoryStream, SaveFormat.Pdf);

    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
}

This code will output:

881
3
2
3
3

On the first run Aspose.Words initializes resources, like fonts required for rendering document to PDF. UpdatePageLayout does not make any difference because it is called internally when document layout is required - when document is saved to Fixed Page formats (PDF, XPS, Image, PS etc) or if it is required to evaluate index fields (like TOC or PAGE).

As a workaround, I can suggest you to run code like this on your application start. This will init the required resources and the first real call will not take long time.

Document doc = new Document();
doc.UpdatePageLayout();

Disclosure: I work at Aspose.Words team.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM