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

Free Support Forum - aspose.com

How to Check Header is Linked to Previous Header using .NET

I still don’t understand whether there is a header:
For testing purposes, I have two documents, the first ww. Docx, with no headers or footers at all,
This second document hh.Docx has a header, but the header is linked to the previous section.
In both cases, the document header returns null, The same returns result makes it impossible for me to distinguish between no header and with header, but the header links to the previous section.

Aspose.Words.Document docc = new Aspose.Words.Document(“c:\ww.docx”); Aspose.Words.Document doch = new Aspose.Words.Document(“c:hh.docx”);

this is Screenshot of test code results.
the first ww. Docx, with no headers or footers at all,
docc.Sections[1].HeadersFooters[HeaderFooterType.HeaderPrimary] is null
docc.Sections[1].HeadersFooters[HeaderFooterType.HeaderFirst] is null

This second document hh.Docx has a header, but the header is linked to the previous section.
doch.Sections[1].HeadersFooters[HeaderFooterType.HeaderPrimary] is null
doch.Sections[1].HeadersFooters[HeaderFooterType.HeaderFirst] is null

Attached(Screenshot.docx) is a screenshot of the test resultshh.docx (18.2 KB)
ww.docx (11.9 KB)
Screenshot.docx (72.0 KB)

@lovecomputer

If the header/footer of section is null and it has previous section with header/footer, the header/footer of section is linked to previous section. Please use following code snippet to check this case. Hope this helps you.

if(doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary] != null && 
    doc.Sections[1].HeadersFooters[HeaderFooterType.HeaderPrimary] == null)
Console.WriteLine("Header Footer is linked to previous header and footer.");

I want to insert a new footer in the second section of the document. The footers should be in Arial font with font size of 9. The footers should be displayed in the center at the bottom of the page. The new footers should not be linked with the previous section
There are three cases:

  1. The current section has a footer and links with the previous section’s footers;
  2. The current section has footers that are not linked with the previous section’s footers;
  3. The current section has no footers.
    In these three cases, I need to delete or edit the original footers and re insert new footers. The footers should be in Arial font with font size of 9. The footers should be displayed in the center at the bottom of the page, and the new footers should not be linked with the previous section

@lovecomputer

Please use the following code example to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");
//Get the second section of document. 
Section section = doc.Sections[1];
//Unlink all headers and footers to the corresponding 
//headers and footers in the previous section. 
section.HeadersFooters.LinkToPrevious(false);
//Create the Footer if it does not exist. 
//If exists, remove its children nodes
HeaderFooter header = section.HeadersFooters[HeaderFooterType.FooterPrimary];
if (header == null)
{
    header = new HeaderFooter(section.Document, HeaderFooterType.FooterPrimary);
    section.HeadersFooters.Add(header);
}
else
{
    header.RemoveAllChildren();
}

//Move the cursor to the primary footer of section section 
//and insert the desired content. 
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToSection(doc.Sections.IndexOf(section));
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Font.Name = "Arial";
builder.Font.Size = 9.0;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("Text of primary footer");
builder.MoveToDocumentEnd();

doc.Save(MyDir + "output.docx");