I have a main function that I'm testing the logic in. Inside of this main function are calls to the methods you mentioned to us previously: GetPageNumberOfSection(Section) and CopyLinkedHeadersFooters(Document).
private static void MainTestingFunction(Document doc, string outputFile)
{
using (var mem = new MemoryStream())
{
CopyLinkedHeadersFooters(doc);
doc.Save(mem, SaveFormat.Pdf);
mem.Position = 0;
var fileInfo = new PdfFileInfo(mem);
int startingPageNumberOfThisSection = 1;
int numberOfPagesInThisSection = 1;
Dictionary pages = GetNumberOfPagesInSections(doc);
int sectionCounter = 1; //debugging only
int totalPages = 0;
//Add the number of pages in each section together for a total page count
foreach (Section currentSection in doc.Sections)
{
totalPages = totalPages + pages[currentSection];
startingPageNumberOfThisSection = GetPageNumberOfSection(currentSection);
numberOfPagesInThisSection = pages[currentSection];
}
//Gets a total page count for the document by adding the starting page number
//of the last section and the number of pages in that section
int diffTotalPages = startingPageNumberOfThisSection + numberOfPagesInThisSection - 1;
//Array to store print commands. Assuming one print command per page.
//If there isnt a new print command (in the header of the current section)
//use the previous print command.
string[] headerInfoArray = new string[diffTotalPages +1 ];
foreach (Section currentSection in doc.Sections)
{
numberOfPagesInThisSection = pages[currentSection];
startingPageNumberOfThisSection = GetPageNumberOfSection(currentSection);
currentHeaderInfo = GetHeaderInfo(currentSection);
int stringCompareValue = String.Compare(currentHeaderInfo, emptyHeaderInfo);
//if the current header is empty (matches a blank string), use previous header info
if (stringCompareValue == 0)
currentHeaderInfo = previousHeaderInfo;
//Add header info to the array starting at the index startingPageNumberOfThisSection
//and continue adding for numberOfPagesInThisSection pages
headerInfoArray = AddHeaderInfoToArray(headerInfoArray, numberOfPagesInThisSection, startingPageNumberOfThisSection, currentHeaderInfo);
previousHeaderInfo = currentHeaderInfo;
sectionCounter++; //debug only
} // for each
//Multiple pages were skipped
//Created a method that replaced skipped pages
headerInfoArray = FixNullHeaders(headerInfoArray);
fileInfo.SaveNewInfo(outputFile);
}
}
///
/// Aspose method of getting first page number of the section.
///
/// The current section to get a starting page number
/// The starting page number of this section
public static int GetPageNumberOfSection(Section section)
{
Document doc = (Document)section.Document;
DocumentBuilder builder = new DocumentBuilder((Document)section.Document);
builder.MoveTo(section.Body.FirstParagraph);
Field pageField = builder.InsertField("PAGE", "1" );
doc.UpdateFields();
doc.UpdatePageLayout();
string pageResult = pageField.Result;
int pageNum = int.Parse(pageField.Result);
pageField.Remove();
return pageNum;
}
///
/// Gets the header info for the current section.
/// Looks in each headerfooter object in the current section
/// for run text matching against a "PRINT" regex string
///
/// The current section to search through
/// Either a matched print command or an empty string</returns
public static string GetHeaderInfo(Section currentSection)
{
string headerInfo = "";
HeaderFooterCollection collectionOfHeaderFooters = currentSection.HeadersFooters;
int headercounter = 1; //debugging only
foreach (HeaderFooter headerfooter in collectionOfHeaderFooters)
{
if (headerfooter.HeaderFooterType == HeaderFooterType.HeaderFirst)
{
if(headerfooter.FirstParagraph.HasChildNodes)
{
if (Regex.IsMatch(headerfooter.FirstParagraph.Runs[0].Text, regExPattern))
{
return headerfooter.FirstParagraph.Runs[0].Text;
}
}
}
headercounter++; //debug only
}
return headerInfo;
}
}
//Adds currentHeaderInfo the array. Each entry in the array corresponds to the page number in the document
//ie. The value in index 8 is the print command that should be applied to page 8
private static string[] AddHeaderInfoToArray(string[] headerInfoArray, int numberOfPagesInThisSection, int startingPageNumber, string currentHeaderInfo)
{
//For each page in the section
for (int i = 0; i < numberOfPagesInThisSection; i++)
{
//Copy the header info (Print command) to the array
headerInfoArray[startingPageNumber] = currentHeaderInfo;
startingPageNumber++;
}
return headerInfoArray;
}