Free Support Forum - aspose.com

Performance Concerns while using AppenField

Below is the piece of code to create Page Number in headerprimary.
I'm using two append fields in below code whereas it takes around "1.5seconds" for those two lines alone to get executed. Thus affecting the overall processing time significantly.
Please address this concern and provide alternatives if any . Attached sample output for your reference.


Document doc = new Document();
Section currentSection = new Section(doc);
doc.AppendChild(currentSection);
Body body = new Body(doc);
currentSection.AppendChild(body);
currentSection.PageSetup.SectionStart = SectionStart.Continuous;

Section defaultSection = new Section(doc);
doc.Sections.Add(defaultSection);
PageSetup ps1 = defaultSection.PageSetup;
ps1.PaperSize = PaperSize.Letter;
ps1.Orientation = Orientation.Portrait;
ps1.TopMargin = 20;
ps1.HeaderDistance = 20;
defaultSection.PageSetup.PageNumberStyle = NumberStyle.Arabic;

defaultSection.PageSetup.RestartPageNumbering = false;
defaultSection.PageSetup.HeaderDistance = 20;

defaultSection.AppendChild(new Body(doc));

HeaderFooter headerPrimary = new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
defaultSection.HeadersFooters.Add(headerPrimary);
//To set Page Number for the document.

Paragraph paraNum = new Paragraph(doc);
paraNum.ParagraphFormat.Alignment = ParagraphAlignment.Center;
Run rntext1 = new Run(doc, "Page");
paraNum.AppendChild(rntext1);

var watch = System.Diagnostics.Stopwatch.StartNew();
paraNum.AppendField(FieldType.FieldPage, true);
watch.Stop();

var elapsedMs = watch.ElapsedMilliseconds;

Run rntext2 = new Run(doc, " Of ");
paraNum.AppendChild(rntext2);

var watch1 = System.Diagnostics.Stopwatch.StartNew();
paraNum.AppendField(FieldType.FieldNumPages, true);
watch1.Stop();

var elapsedMs1 = watch1.ElapsedMilliseconds;

var totaltime = elapsedMs + elapsedMs1;

Paragraph ptime = new Paragraph(doc);
Run rntimedesc = new Run(doc, "Total time taken to Process \"Append Fields\" for HeaderPrimary Page Number: ");
ptime.AppendChild(rntimedesc);
Run rntime = new Run(doc, totaltime.ToString()+" milliseconds");
rntime.Font.Bold = true;
rntime.Font.Size = 15;
rntime.Font.Underline = Underline.Single;
ptime.AppendChild(rntime);
currentSection.Body.AppendChild(ptime);
headerPrimary.AppendChild(paraNum);

doc.Save("TestingHeaders.docx");
Hi Manikandan,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for .NET 16.12.0 and have not found the shared issue. Please use Aspose.Words for .NET 16.12.0. We have attached the output document with this post for your kind reference.