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

Free Support Forum - aspose.com

Page Numbering and Sections

Hi,


Interesting situation. There are a number of sections. There is a business requirement that in a word document’s footer, there must be page numbering throughout entire document and each section. Furthermore, this situation get’s more complicated in that the first footer and second footer of a section can be different. In small documents, pretty fast; however, in large documents when the number of sections start to get large, the amount of time it takes to complete, increases.

To add page number for each sections is done as follow: builder.InserField(“PAGE \* MERGEFORMAT”);

For the entire document, the page numbering is accomplished by adding pages per section. Please see following code except:
foreach (string sect in sectionNames)
{
builder.MoveTo(field.Separator);
builder.Write(" + ");
builder.InsertField(“PAGEREF " + sect + " \# “0” \* MERGEFORMAT”);
}

Please see attached document for code being used (demo code.cs) as well as input document (demo of issue.docx) and output document (demo of issue2.docx).

If anyone has encountered a similar situation, did you have any ways to resolve the time faster? Thanks

I’m hoping there is a faster way to add fields, if anyone knows how

Hi Aaron,

Thanks for your inquiry.

After an initial test with the latest version of Aspose.Words 13.7.0, I was unable to reproduce this issue on my side. I executed your code on a 64-bit Windows 7 machine and it took 4100 milliseconds for the first run, 2865 milliseconds for the second run and 2791 milliseconds for the third run. So, in case you're using an old version, I would suggest you please upgrade to the latest version of Aspose.Words from the following link:
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

I hope, this improves the performance.

Best regards,

Hi,


I got latest version. Time did not improve in running code. I conducted some analysis. Here is what I found

section Pages per section FooterFirst
(in seconds)
FooterPrimary
(in seconds)
1 1 0 0
2 1 2 1
3 3 3 3
4 4 5 4
5 4 8 6
6 4 8 9
7 1 13 12
8 9 12 11
9 6 21 19
10 4 15 13
11 5 16 17
12 4 19 22
13 1 19 22
14 4 23 24
15 8 22 24
16 3 30 24
62 216 211

This is the time it took for a document that had 62 pages in 16 sections. This document has numerous bookmarks, etc. Furthermore, I mocked up the code to simulate the 16 sections with various pages per section. It is included as an attachment. When this was run through code, it took 139 seconds to run. My guess is that with all the added document features, this is what takes so much time.

Here is the 10 lines of code that was added.
sectionNames.Add("s7");
sectionNames.Add("s8");
sectionNames.Add("s9");
sectionNames.Add("s10");
sectionNames.Add("s11");
sectionNames.Add("s12");
sectionNames.Add("s13");
sectionNames.Add("s14");
sectionNames.Add("s15");
sectionNames.Add("s16");

Aaron

Hi Aaron,


Thanks for your inquiry.

Most likely what is causing the slowdown is that the first overload of InsertField will update the field at the same time, with PAGE field this can often mean building the page layout in memory which although fast, in this case might stack up and be causing the slow down you are experiencing.

To try solve the issue please replace any instance of builder.InsertField(“PAGE”); with builder.InsertField(“PAGE”, “1”); and see if that helps.

Please let me know how you go.

Cheers,

Thanks for the suggestion; however, in the metrics I gathered, the longest portion of time occurred around the following code:


foreach (string section in sections)
{
builder.MoveTo(field.Separator);
builder.Write(" + ");
builder.InserField(“PAGEREF " + section + " \# “0” \* MERGEFORMAT”);
}

Hi Aaron,


In addition to what Adam has suggested, in your case, you shouldn’t call Document.UpdateFields method many times inside a loop either. I have modified your code (please see the attachment) to improve performance and the modified code still produces the same output. If we can help you with anything else, please feel free to ask.

Best regards,

Thanks for the suggestion. The strategy for page numbering was changed. Copying footers and modifying the copied footer

Hi Aaron,


In case you have any further inquires or need any help, please let us know. We’re always glad to help you.

Best regards,