We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Performance improvement while generating word document

bool isCustomPropertyAdded = false;
foreach (Aspose.Words.Properties.DocumentProperty prop in GenratedDocFrag.CustomDocumentProperties)
{
    if (MasterTemplateDoc.CustomDocumentProperties.Contains(prop.Name) == false)
    {
        MasterTemplateDoc.CustomDocumentProperties.Add(prop.Name, prop.Value.ToString());
        isCustomPropertyAdded = true;
    }
}
if (isCustomPropertyAdded)
{ MasterTemplateDoc.Range.UpdateFields(); }

To append the document with the Master Template the code used is:

foreach (Aspose.Words.Section srcSection in GenratedDocFrag)
{ 
    Aspose.Words.Section newSection = (Aspose.Words.Section)MasterTemplateDoc.ImportNode(srcSection, true, ImportFormatMode.UseDestinationStyles); 
    MasterTemplateDoc.LastSection.AppendContent(newSection); 
}

Replace the variable value by values in the XML file using the following code in the final document(combination of Matser and the document):

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(variablecollection);
foreach (XmlNode node in xmldoc.SelectNodes("//Variable"))
{
    if (node.Attributes["Name"].Value.ToString() != node.InnerText)
    {
        this.PutVariablesValueInMasterDocument(MasterTemplateDoc, node.Attributes["Name"].InnerText, node.InnerText);
    }
}
this.PutVariablesValueInMasterDocument(MasterTemplateDoc, "var_document_name", documentName);
this.PutVariablesValueInMasterDocument(MasterTemplateDoc, "var_solution_name", solutionName);
this.PutVariablesValueInMasterDocument(MasterTemplateDoc, "var_version", version);
MasterTemplateDoc.BuiltInDocumentProperties["Author"].Value = UserManager.UserMasterThisUser.UserName;
MasterTemplateDoc.Range.UpdateFields();
private void PutVariablesValueInMasterDocument(Aspose.Words.Document masterDocument, string variableName, string variableValue)
{
    if (masterDocument.CustomDocumentProperties.Contains(variableName))
    {
        masterDocument.CustomDocumentProperties[variableName].Value = variableValue;
    }
}
``
After all the above steps we are inserting footer in the final document
```cs
MasterTemplateDoc = insertFooterInDocument(solutionName, documentName, MasterTemplateDoc);
private Document insertFooterInDocument(string strSolutionName, string strDocumentName, Document MasterFile)
{
    DocumentBuilder builder = new DocumentBuilder(MasterFile);
    Aspose.Words.Section currentSection = builder.CurrentSection;
    Aspose.Words.PageSetup pageSetup = currentSection.PageSetup;
    pageSetup.DifferentFirstPageHeaderFooter = true;
    builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
    builder.Write(strSolutionName + " - " + strDocumentName);
    builder.Write(" Page ");
    builder.InsertField("PAGE", "");
    builder.Write(" / ");
    builder.InsertField("NUMPAGES", "");
    builder.CurrentParagraph.ParagraphFormat.StyleIdentifier = StyleIdentifier.Footer;
    builder.MoveToDocumentEnd();
    return MasterFile;
}

Save the final document at a location on the File System

MasterTemplateDoc.Save(outputFileName);

This steps (1-4) are repeated for 4-5 times then our final Solution is created.
Now when 50 users simultaneously request for generating the solution using above approach the CPU usage goes high (100%) and the perfomance goes very low as a result of which 30-35 solutions are generated and the rest are failed. So Can you please give us suggestion for the following:
how we can optimize the CPU performance
how can we optimize the above code
Whenever we are creating doc object using Aspose it is consuming memory. How we can avoid this or how we can bring down the memory usage
Can you advise on what is the best method to dispose of your components in the code, so that we can prevent it from holding memory resources when they are no longer needed ?
Please provide the solutin asap.
Thanks,
Samanvay

Hi
Thanks for your request. 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. But anyways I will try to give some recommendations:
1 and 2. Have you considered using Mail Merge approach to fill your documents with data? I think, this can simplify the process:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Also, in your code, you are merging your documents into one large document, then replace document properties and then update fields. Since upon updating fields, Aspose.Words rebuilds document layout, this operation can be quite CPU and memory consuming if your document is large. So I would suggest you to replace document properties in each particular document, update fields in them and then merge them into one large document. I cannot guaranty this will make process much faster, but it will be good to try.
3. Again, memory usage fully depends on document complexity, size and format of input file. Usually, Aspose.Words needs 10 times more memory than the original document size to build a DOM in the memory.
4. It is not necessary to dispose Aspose objects. The memory should be released once the Document object is not used. If you are using streams, please make sure that you dispose your streams when they are not used.
Best regards,

Thanks for your replay but we have to optimize our CPU performance because if we use aspose then CPU utilization is 100% as a result of which server runs very slow and performance of application is decrease.

So, Is there any way to optimize CPU utilization using aspose.

Hi
Thanks for your request. The only way to “optimize CPU performance” is using smaller and simpler documents or avoid performing complex operations on large and complex documents. In your case, UpdateFields operation is the most time, memory and CPU consuming operation.
Best regards,