AppendDocument Page Numbering Issues

Hello,
I am trying to take a template, loop through a process of finding and replacing certain fields and then merge each individual document that is created into a blank master document.
The template has 2 pages, and I am creating 3 documents to be merged
into the master document for a total of 6 pages.
My problems are with the page numbers.

I found the following post (https://forum.aspose.com/t/78349) and have tried to get the suggested code to work, but have little success.

  1. When I at first open this Word document, the page numbers on the first page in the Header & Footer are wrong, but as soon as I scroll down to the second page, they correct themselves. Why are they not right when I open the document? I tried using the Document.UpdatePageLayout() method before saving the master document,

but have had zero success. They do re-generate properly when exporting to PDF, but only after the document has been created.

  1. My ultimate preference is to have the page numbers be 1/2, 2/2, 1/2, 2/2, 1/2, 2/2, 1/2, 2/2 instead of 1/6, 2/6, 3/6, etc. How can I perform this? I have tried the UnlinkFields method that was suggested in the forum, but didn’t have any success with that either. The code would only update some of the fields in the header and I couldn’t get it to update the fields in the footer.

Attached are some of the files including a demo app. Please let me know what else you need.

Thank you for your assistance!

Hi Aaron,

Thanks for your request. If your documents consist of only one section, it is very easy to achieve. In MS Word, you can use SECTIONPAGES field to display number of pages in the section. In case, when your document consists of only one section, you can use this field instead of NUMPAGES field. So the only thing you should do is restart page numbering in each section using RestartPageNumbering property:
https://reference.aspose.com/words/net/aspose.words/pagesetup/restartpagenumbering/
But you should note, such approach will work only if your documents consists of only one section.
In case of multiple sections document, you should use approach suggested in the thread you have pointed to. You should unlink NUMPAGES field in order to prevent its updating and set RestartPageNumbering property of the first section of each subdocument.
Best regards.

Hello Alexey,
Thank you for your quick response!

Unfortunately I do not have any control over the format of the templates. They are coming from the client and could have any number of section (header, footer, body) configurations. This will void the restartpagenumbering concept, although I did see the post and take it into consideration.

I am trying to catch the page number combo that will be most likely with Page/NumofPages.

I also attempted the UnlinkFields method (see my attached solution), but it also failed and I had various problems with it…
Use it one way and it will unlink the NumPages field in the Header, but not the Footer. Try to run it for the Page field and it will only do the Header. Try to search for both fields and it will only do the Page field and miss the NumPages field. If I run it first and search for NumPages, it will find them for the Header, run it again and find the Pages in the Header, run it a third time and get the NumPages in the Footer, fun it a Fourth time and get the Page field in the Footer…but only if the template is one page long…I can’t win

Any other ideas or suggestions?

Hi

Thanks for your request. Could you also attach your document here for testing. Also, please try modifying your code as shown below:

Public Sub UnlinkFields(ByRef doc As Document)
Dim starts As NodeCollection = doc.GetChildNodes(NodeType.FieldStart, True)
Dim startsArray As Node() = starts.ToArray()

For Each start As Fields.FieldStart In startsArray

Hope this helps.
Best regards.

See attached.

I modified the code and it worked fine for the NumPages field, so I added an “OrElse” to check for Page field and it causes the pages numbers to be 1/2, 1/2, etc for each page when it should be 1/2, 2/2, 1/2, 2/2 etc.

Public Sub UnlinkFields(ByRef doc As Document)
Dim starts As NodeCollection = doc.GetChildNodes(NodeType.FieldStart, True)
Dim startsArray As Node() = starts.ToArray()

For Each start As Fields.FieldStart In startsArray
If start.FieldType = Fields.FieldType.FieldNumPages _
OrElse start.FieldType = Fields.FieldType.FieldPage Then

Hi Aaron,

Thank you for additional information. But such trick will not work for PAGE field. Here you should restart page numbering in the first sections of your sub documents using RestartPageNumbering property:
https://reference.aspose.com/words/net/aspose.words/pagesetup/restartpagenumbering/
Best regards.

The RestartPageNumbering method words to get the Page field set to the right number, but it’s still a field.

If I run the Unlink method afterwords, it will unlink the NumPages field and then I have one field and one fixed text. Which works in a sense, but not quite the way we would prefer.

How about the other original question I asked.
When I do the merge with out touching either Page Number fields, and I open the master document I see Page 1 of 3 on the first page, but when I scroll to the second it’s 2 of 6 and when I scroll back to the first it’s 1 of 6 as it should be. I have tried the UpdatePageLayout() method, but it doesn’t work.

You use the application I attached earlier and just comment out the UnlinkFields method call.

Thanks,

Hi

Thank you for additional information. You cannot replace PAGE field with simple text because its propose is displaying current page number. If you replace it with simple text, the same value will be displayed on each page.
Regarding your other original question, most likely this occurs because MS Word updates fields when you scroll document down.
Best regards.