Total page number is incorrect when multiple master pages are used

Hi Team,

When I apply different master pages to page 1 and page 2, the total number of pages is incorrect for the first page.

This is what I write in the footer of both the master pages (1 of N).

However, for page 1, it prints “1 of 1”.
for page 2, it print, “2 of 2”.

When I open the document and double click on footer text “1 of 1”, it updates to “1 of 2”.

Thanks,
Kumar

public class TestDifferentMP
{
    public static void main(String[] args) throws Exception
    {
        TestAsposeUtils.setupLicense(TestAsposeUtils.LICENSE_FILE_PATH);

        Document doc = new Document();
        DocumentBuilder docBuilder = new DocumentBuilder(doc);

        // write footer
        addFooterPageNumber(doc, docBuilder);
        docBuilder.write("Page 1");

        docBuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
        doc.getLastSection().getHeadersFooters().linkToPrevious(false);

        addFooterPageNumber(doc, docBuilder);
        docBuilder.write("Page 2");

        // doc.updateFields();
        // doc.updatePageLayout();

        String baseFolder = System.getProperty("java.io.tmpdir");
        String unique = UUID.randomUUID().toString().replaceAll(" ", "_");
        String docpath = baseFolder + "test_" + unique + "_" + BuildVersionInfo.getVersion(); //$NON-NLS-1$ //$NON-NLS-2$
        doc.save(docpath + ".doc"); //$NON-NLS-1$
        System.out.println(docpath + ".doc");
    }

    private static void addFooterPageNumber(Document doc, DocumentBuilder docBuilder) throws Exception
    {
        Node curNodeInDocument = docBuilder.getCurrentNode();
        if (curNodeInDocument == null)
        {
            curNodeInDocument = docBuilder.getCurrentParagraph();
        }

        docBuilder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);

        docBuilder.insertField("PAGE", ""); //$NON-NLS-1$ //$NON-NLS-2$
        docBuilder.write(" of ");
        docBuilder.insertField("NUMPAGES", ""); //$NON-NLS-1$ //$NON-NLS-2$

        if (curNodeInDocument != null)
        {
            docBuilder.moveTo(curNodeInDocument);
            curNodeInDocument = null;
        }
        else
        {
            docBuilder.moveTo(doc.getLastSection().getBody().getLastParagraph());
        }
    }
}

Hi Kumar,
We were able to reproduce the issue at our end. This issue has been logged into our issue tracking system as WORDSNET-12213. We will keep you updated on this issue in this thread.
Best Regards,

Hi Muhammad,

This issue is very strange. Is there any quick workaround for this? The work around through some word setting or through macro solution?

Additionally, when could I expect a fix for this?

Thanks
Kumar

Hi Kumar,
The issue is in analysis phase and we will be able to share more details once the analysis is complete. As a workaround, you can use BreakType.PAGE_BREAK instead of section break on the following line.
docBuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
Best Regards,

Hi Muhammad,

PAGE_BREAK causes another issue. Where the page number details are printed twice. Looks like with PAGE_BREAK, the header/footer data will be same for different master pages. This is not acceptable.

You could try it out yourself the see the below issue.

1 of 21 of 2
Thanks,
Kumar

Hi Kumar,
In your example, you were adding two sections and calling addFooterPageNumber method twice (first for first section and then for second section). If you want to use this workaround and insert a page break instead of a section break, you will have to call addFooterPageNumber method once because now you have only one section in your document.
If this workaround is not acceptable in your case, you can leave it and wait for a fix by our product team.
Sorry for the inconvenience.
Best Regards,

Hey Muhammad,

>>> If this workaround is not acceptable in your case, you can leave it and
No. This work around is not acceptable as we have a generic solution and this specific scenario doesn’t fit for generic solution.

>>> wait for a fix by our product team.
When can I expect a fix?

Thanks,
Kumar

Hi Kumar,
The issue has been analyzed by our product team and according to our investigation there is no issue with Aspose.Words in this case. Following is the detail of our analysis.
This is a MS Word issue. The *.doc and *.docx formats do not contain value of layout fields (like PAGE and NUMPAGE) located in header/footer of each page. Usually, MS Word updates these fields automatically when document is opened in MS Word, but does not in this case.
A similar document is created with MS Word to show how MS Word behaves in this case. Document is attached for your reference.
Best Regards,

Hi Muhammad,

I tried creating it myself in MS Word but I could not succeed. Please let me know the step to replicate the issue from MS Word manually.

Thanks,
Kumar

Hi Kumar,
A request has been forwarded to our product team. We will share detailed steps with you soon.
Best Regards,

Hi Kumar,
These were the steps followed to create sample document:

  1. Create new document
  2. Insert section break (next page)
  3. Edit first page footer
  4. Insert fields: { PAGE } { NUMPAGES }
  5. Unlink second page footer from previous.
  6. Save document

Best Regards,

Hi Muhammad,

Two questions.

  1. Why is PDF output good?
  2. How do I fix the code I shared in initial thread? Setting linkToPrevious(true) is not helping.

Thanks,
Kumar

Hi Kumar,
Thanks for the details. We are further investigating it and will share our findings with you soon.
Best Regards,

Hi Muhammad,

Thanks.

See the solution provided on MS Word forum - https://www.msofficeforums.com/word/27380-word-does-not-update-numpages-when-multiple.html

thanks,
Kumar

Hi Kumar,
In fact setting linkToPrevious() to true resolves this issue for Aspose.Words also. You should call this method after adding the second page footer as you can see in the following code.

Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);

// write footer
addFooterPageNumber(doc, docBuilder);
docBuilder.write("Page 1");
docBuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// doc.getLastSection().getHeadersFooters().linkToPrevious(true);
addFooterPageNumber(doc, docBuilder);
docBuilder.write("Page 2");
doc.getLastSection().getHeadersFooters().linkToPrevious(true);

Best Regards,

Hi Muhammad,

Thanks. I realized it later.

Let me know once you get answer for “1. Why is PDF output good?”

Thanks,
Kumar

Hi Kumar,
When you save as PDF, page layout is updated again and you do not see the issue in the output PDF.
Best Regards,