Document takes a long time to save

  1. Take a look at the piece of code below.
private static string GetFilterListForWordTemplate(byte[] binTemplate, int intCustomerID, PYWUser userInfo, out List listBOs, out Aspose.Words.Document document, Dictionary keyVal, DataSet dsTableBOs, Dictionary dictImages, Dictionary dictSections)
    string strReturnValue = "";
    listBOs = new List();
    string strBOPrefix
    string strReportsPath = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("ReportsPath");
        document = new Aspose.Words.Document(new MemoryStream(binTemplate));
        document.MailMerge.FieldMergingCallback = new HandleMergeField(keyVal.ToDictionary(kv => kv.Key, kv => kv.Value.ToString()), dsTableBOs, dictImages, dictSections);
        // Call execute mail merge for all field names passing the values we want to substitute
        document.MailMerge.Execute(keyVal.Keys.ToArray(), keyVal.Values.ToArray());
        // Call accept which uses a visitor pattern to visit each section of the document and where we will intercept leftover section boundaries and selectively delete them
        document.Accept(new RemoveEmptyRegions(false, strSectionStartPrefix, strSectionEndPrefix));
        document.Accept(new RemoveEmptyRegions(false, strTableStartPrefix, strTableEndPrefix));
        // document.AcceptAllRevisions();
        // The line below doesnt work because there appears to be a bug in saving directly to pdf where the sections do not get hidden from the previous steps so instead saving as doc and converting in two steps
        // document.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strReportsPath + @"\Kyryll " + DateTime.Now.Millisecond + ".pdf"), SaveFormat.Pdf);
        MemoryStream msTempMemStreamDocx = new MemoryStream();
        if (boolSaveIntermidiateDocs)
            document.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log", "Report for " + intCustomerID + ".docx"), SaveFormat.Docx);
        document.Save(msTempMemStreamDocx, SaveFormat.Docx);
        document = new Aspose.Words.Document(msTempMemStreamDocx);
        string strDocumentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strReportsPath, intCustomerID.ToString());
        if (!Directory.Exists(strDocumentPath))
        strDocumentPath = Path.Combine(strDocumentPath + @"\Report " + Guid.NewGuid().ToString() + ".pdf");
        document.Save(strDocumentPath, SaveFormat.Pdf);
        strReturnValue = strDocumentPath;
    catch (Exception ex)
        Util.LogAndNotify("Exception", "ReportGeneration", "GetFilterListForWordTemplate", userInfo, ex);
        throw ex;
    return strReturnValue;

In the code snippet above, saving directly to pdf produces strange results. However, saving as doc to memory stream, reopening and saving as pdf is fine. Is there an internal step that’s skipped in Aspose.Words when saving to pdf that is not skipped when saving to docx? This doubles the saving time which is already long as mentioned in point 1.
Your help would be greatly appreciated

Thanks for your request. Could you please also attach your template and data source or create a simple application that will allow us to reproduce the problem? We will check the issue and provide you more information.
Best regards,

Thanks for the prompt reply.

I have attached a sample project. If you enter the text PYWMaster-Light.docx in the text box in the window and click the save button you will see that it takes 3 secs on the last method when saving to pdf. This seems like a long time for a 2MB file.


Thank you for additional information. Unfortunately, I cannot reproduce the problem on my side. I tried saving to PDF directly and with resaving to DOCX as in your code. In both cases output PDF looks the same.
Regarding long saving time, it is expected that saving to PDF, XPS or any other fixed page format, takes more time than saving to flow format (DOC, DOCX etc). This is because MS Word document is flow document, i.e. it does not contain any information about its layout into pages. So to convert it to PDF, Aspose.Words needs to layout it into pages. This is quite complex operation and that is why it takes more time.
Best regards,