Different page number after append a document for the first page

Hi all,

We have a document which when we have appended a document in front of it, the page number also updated. Just see the attached PDF, the cover page is attached using the program, so the second page page number was changed. But we don’t want to change the page number. So how can we keep the page number no change?

Regards,
Ken Wan

Hi Ken,

Thanks for your request. Page numbers and total pages are represented by PAGE and NUMPAGES fields in MS Word documents. During converting to PDF these field are updated. That is why you see actual page numbers in the output document.
If you need to keep the original page numbering, the simplest way to achieve this is converting your documents to PDF separately and then concatenate PDFs. You can concatenate PDF documents using Aspose.Pdf.Kit.
https://docs.aspose.com/pdf/net/concatenate-pdf-documents/
Another way to resolve this issue is using RestartPageNumbering property:
https://reference.aspose.com/words/net/aspose.words/pagesetup/restartpagenumbering/
But in this case NUMPAGES field will show the actual number of pages in the document.
Best regards.

Hi,

So for the second case, how to set the total number of pages?

Regards,
Ken Wan

Hi

Thanks for your request. As you know, Aspose.Words allows getting number of pages in the document (Document.PageCount property). So you can find PAGE fields in your document and replace them with simple text (number of pages). Here is code example, which you can use to unlink PAGE fields:

private void UnlinkFields(Document doc)
{
    NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
    foreach(FieldStart start in starts)
    {
        if (start.FieldType == FieldType.FieldNumPages)
        {
            Node currentNode = start;
            Node fieldSeparator = null;
            while (currentNode.NodeType != NodeType.FieldSeparator && currentNode.NodeType != NodeType.FieldEnd)
            {
                currentNode = currentNode.NextSibling;
                currentNode.PreviousSibling.Remove();
            }
            if (currentNode.NodeType == NodeType.FieldSeparator)
            {
                fieldSeparator = currentNode;
                while (currentNode.NodeType != NodeType.FieldEnd)
                {
                    currentNode = currentNode.NextPreOrder(doc);
                }
                fieldSeparator.Remove();
            }
            currentNode.Remove();
        }
    }
}

Hope this helps.
Best regards.

Hi,

Thank you for your code. But at the same time, we used section in word to control pages. That means we will delete some section, and duplicate some section, which will control a page show or not show up. Then it fails after.

Attached is the failed PDF. You can see the total page should be 8, but it shows 17, which is the original size of the template.

Regards,
Ken Wan

Hi Ken,

Thank you for additional information. It would be great if you attach your source documents and create a simple application, which will allow me to reproduce the problem on my side. I will check your code and documents and provide you more information.
Best regards.

Hi,

Attached is the source code and word template.

Regards,
Ken Wan

Hi,
Attached is a simplified sample applcation and source code.
Regards,
Ken Wan

Hi Ken,

Thank you for additional information. I think, in your case, you should call Document.UpdatePageLayout method before unlinking NUMPAGES fields. Please see the following code snippet:

doc.UpdatePageLayout();
UnlinkFields(doc);

Hope this helps.
Best regards.

Hi,

Two questions:

  1. There is a problem that there is a limitation that we have to append the cover in front of the template first, and then delete some page inside. So, how can we count the total number of page without the cover? Now we use UnLinkField function, the total number of page included the cover

  2. Can you briefly describe what’s the UnLinkField function doing? I don’t understand

Hi Ken,

Thanks for your request. As I already mentioned earlier, page numbers and total number of pages in MS Word documents are represented by fields. When you save the output document to PDF these fields are updated, so you see the actual numbers.
In your case, you would like to concatenate few documents, but you do not want to show the actual numbers. You need to display numbers and total number of pages as they were in the original document. So, to achieve this we need to prevent updating fields before saving the document.
To achieve this we “unlink” NUMPAGES fields in your documents. Unlinking means converting field to simple text. Each field in MS Word consists of FieldStart, FieldSeparator, FieldEnd nodes and Run nodes, which represent field code and field value (displayed text). So in MS Word document field looks like the following:
[FieldStart]here is field code[FieldSeparator]here is field value[FieldEnd]
In case of NUMPAGES field, it looks like the following:
[FieldStart] NUMPAGES [FieldSeparator]10[FieldEnd]
NUMPAGES is field code (you can see it if pres Alt+F9), and 10 is number of pages (text, which you see in the document). You can also open your document using DocumentExplorer (Aspose.Words demo application) to investigate your document’s structure.
When you run UnlinkFields method, it searches for NUMPAGES fields in the document and remove all field’s nodes except field value. So the field becomes a simple text and cannot be updated.
Hope my explanation could help you.
Best regards.

Hi,

Thank you very much. You explanation is very clear.

So I want to ask how to set the page number? As now our practice is append document, and then delete some page. We can’t update the page number directly. So, is that we have to set page number by ourself?

If yes, how to set the page number?

Hi Ken,

Thanks for your request. If you need that number of pages were not updated according to actual number of pages in the document, you should insert a hardcoded value as simple text.
Best regards.

Hi,

I tried to change the page number by the following code and template. But it fails to fill in the totalPageNum using pageCount. Do you know why? And how do you control the total page number to be the total number of page minus the cover?

Regards,
Ken Wan

Hi

Thanks for your request. Please try using the following code:

string[] fieldArray = {
    "totalPageNum"
};
object[] mergeFieldValue = new object[1];
Document doc = new Document(@"Test001\PPS_FS_ENG.doc");
Document tmpDoc = new Document(@"Test001\cover.doc");
tmpDoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
tmpDoc.Sections.RemoveAt(3);
mergeFieldValue[0] = tmpDoc.PageCount - 1;
tmpDoc.MailMerge.Execute(fieldArray, mergeFieldValue);
tmpDoc.UpdatePageLayout();
tmpDoc.Save(@"Test001\test.pdf");

Hope this helps.
Best regards.