- 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
System.Web.Configuration.WebConfigurationManager.AppSettings.Get("BOInputPrefix");
string strReportsPath = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("ReportsPath");
try
{
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);
document.MailMerge.ExecuteWithRegions(dsTableBOs);
// 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))
Directory.CreateDirectory(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