We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Different header on first six(6) pages of the document

Hi,

My requirement is to maintain different header for first six pages of the document and common footer for all pages of the document.

From seventh page header will be different and footer will be same as in first six pages.

How can i achieve this by using Aspose.words.

Please share the code.

Regards
Naveen

Hi Naveen,

Thanks for your inquiry and sharing the detail via live chat. Please use following code example to insert section break (continuous) at each page of Word document. Once you have section break at each page, you can easily insert different header/footer on each page.

Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);
int pageIndex = 1;
foreach (Section section in doc.Sections)
{
    foreach (Paragraph para in section.Body.Paragraphs)
    {
        if (collector.GetStartPageIndex(para) == pageIndex)
        {
            builder.MoveTo(para);
            builder.InsertBreak(BreakType.SectionBreakContinuous);
            pageIndex++;
        }
    }
}
// Add Header/Footer to sections
for (int i = 1; i < doc.Sections.Count; i++)
{
    HeaderFooter header = doc.Sections[i].HeadersFooters[HeaderFooterType.HeaderPrimary];
    if (header == null)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
        doc.Sections[i].HeadersFooters.Add(header);
    }
    Paragraph Para = new Paragraph(doc);
    header.AppendChild(Para);
    header = doc.Sections[i].HeadersFooters[HeaderFooterType.FooterPrimary];
    if (header == null)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(doc, HeaderFooterType.FooterPrimary);
        doc.Sections[i].HeadersFooters.Add(header);
    }
    Para = new Paragraph(doc);
    header.AppendChild(Para);
}
doc.Save(MyDir + "Out.docx");