Aspose.Words - AppendDocument shows total pagecount in footer

Hello,
I am facing wired issue. I am using append builder.Document.AppendDocument to merge source document into target document. Target document originally had 5 pages and page numbers on target document started from “0”. Source document footer also has page number with different format. I want to keep the source document footer so I kept LinkToPrevious(false). Source document has total 10 pages. So, in output document after appendDocument it shows 1 of 15, 2 of 15 …
It should show 1 of 10, 2 of 10 etc…

below is my code.

doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
doc.FirstSection.HeadersFooters.LinkToPrevious(false);

doc.FirstSection.PageSetup.RestartPageNumbering = true;
doc.FirstSection.PageSetup.PageStartingNumber = 1;


var formatOption = new ImportFormatOptions();
formatOption.IgnoreHeaderFooter = false;
formatOption.KeepSourceNumbering = true;

doc.FirstSection.PageSetup.PageWidth = dstDoc.LastSection.PageSetup.PageWidth;
doc.FirstSection.PageSetup.PageHeight = dstDoc.LastSection.PageSetup.PageHeight;
doc.FirstSection.PageSetup.Orientation = dstDoc.LastSection.PageSetup.Orientation;


//Iterate through all sections in the source document.
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    para.ParagraphFormat.KeepWithNext = true;
}
builder.Document.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting, formatOption);

@kautilya Page count in MS Word documents usually is represented by NUMPAGES field. The field value is updated by MS Word when you open the document. Since number of pages is changed after appending document, the value of the field is also changed. You can work this around by unlinking (replacing with static text) NUMPAGES in your documents before appending them:

// Unlink NUMPAGES fields in the document.
doc.Range.Fields.Where(f => f.Type == FieldType.FieldNumPages).ToList()
    .ForEach(f => f.Unlink());

I have made that change, it started giving incorrect total page count.
Below is my code change.

doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
doc.FirstSection.HeadersFooters.LinkToPrevious(false);

doc.FirstSection.PageSetup.RestartPageNumbering = true;
doc.FirstSection.PageSetup.PageStartingNumber = 1;

var formatOption = new ImportFormatOptions();
formatOption.IgnoreHeaderFooter = false;
formatOption.KeepSourceNumbering = true;

doc.FirstSection.PageSetup.PageWidth = dstDoc.LastSection.PageSetup.PageWidth;
doc.FirstSection.PageSetup.PageHeight = dstDoc.LastSection.PageSetup.PageHeight;
doc.FirstSection.PageSetup.Orientation = dstDoc.LastSection.PageSetup.Orientation;

doc.Range.Fields.Where(f => f.Type == FieldType.FieldNumPages).ToList().ForEach(f => f.Unlink());

//Iterate through all sections in the source document.
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    para.ParagraphFormat.KeepWithNext = true;
}
builder.Document.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting, formatOption);

@kautilya Could you please attach your input, output nd expected output documents here for our reference? We will check the issue and provide you more information.

@alexey.noskov Apologies, I won’t be able to share the input doc. Is there any other way?

@kautilya You can create dummy documents that will allow us to reproduce the problem. Also, please note, it is safe to attach documents in te forum, only you as a topic starter and Aspose staff can see the attachments.