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