Page Count rendering issue

Hi I have an issue while doing a page count. Essentially I have some fairly stable code working for a few years now and has been working fine. Lately some of my clients want a contents page so my idea was while I was appending multiple documents together I could count the number of pages before documents were appended and I could store these values and create my own custom content page. Weirdly for some reason I do a page count my document changes. The change that occurs is that my header get carried across to every document by default but when I add the lines shown below my header isn't carried across.

doc.UpdatePageLayout();
int a = doc.PageCount;

My actual code as shown below.

MemoryStream startStream = new MemoryStream(files[0].FileData);
Document doc = new Document(startStream);

int[] count = new int[files.Length];


for (int i = 1; i < files.Length; i++)
{


if (files[i].FileData == null)
{
}
else
{
MemoryStream ms = new MemoryStream(files[i].FileData);

// Open the document to join.
Document srcDoc = new Document(ms);


bool pageBreakBefore = files[(i - 1)].PageBreakAfter;

int headerAndFooterCount = srcDoc.FirstSection.HeadersFooters.Count;
bool hasHeaderAndFooter = false;

if (headerAndFooterCount > 0)
{
hasHeaderAndFooter = true;
}

if (pageBreakBefore == true)
{



srcDoc.FirstSection.PageSetup.PageWidth = doc.LastSection.PageSetup.PageWidth;
srcDoc.FirstSection.PageSetup.PageHeight = doc.LastSection.PageSetup.PageHeight;
srcDoc.FirstSection.PageSetup.Orientation = doc.LastSection.PageSetup.Orientation;
}
else
{

srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;


srcDoc.FirstSection.PageSetup.PageWidth = doc.LastSection.PageSetup.PageWidth;
srcDoc.FirstSection.PageSetup.PageHeight = doc.LastSection.PageSetup.PageHeight;
srcDoc.FirstSection.PageSetup.Orientation = doc.LastSection.PageSetup.Orientation;
}


doc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);


if (i > 1)
{


if (hasHeaderAndFooter == true)
{

}

else
{

doc.Sections[i].HeadersFooters.LinkToPrevious(true);
}

}


}

doc.UpdatePageLayout();
int a = doc.PageCount;
}



If the last 2 lines are commented out the document is fine. If the are uncommented I can see the "a" variable is correct in the loop but the document does not render correctly.
Hi Matthew,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word documents.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we'll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click 'Reply' button that will bring you to the 'reply page' and there at the bottom you can include any attachments with that post by clicking the 'Add/Update' button.