Free Support Forum - aspose.com

Detect If any Header is Linked to Previous Section in Word Document | Link All Headers C# .NET

I have three requirements for headers
1、How can I judge if the header is linked to the previous section or not?
2、If the header doesn’t link to the previous section, how can I modify the header text?
For example, the original header text is 123, I want to change it to 146
3、If the header is not linked to the previous section, how can I link the header of this section to the previous section?
Header.docx (15.4 KB)

The following code does not complete my idea

Aspose.Words.Document docc = new Aspose.Words.Document("Header.docx");
DocumentBuilder builder1 = new DocumentBuilder(docc);
Aspose.Words.HeaderFooter hf;
hf = builder1.Document.Sections[0].HeadersFooters[HeaderFooterType.HeaderPrimary];
bool hf_link = builder1.Document.Sections[0].HeadersFooters[HeaderFooterType.HeaderPrimary].IsLinkedToPrevious;
if (!hf_link)
{
builder1.Document.Sections[0].HeadersFooters[HeaderFooterType.HeaderPrimary].Range.Text.Replace("123" , "146");
builder1.Document.Sections[0].HeadersFooters[HeaderFooterType.HeaderPrimary].IsLinkedToPrevious = true;
    builder1.Document.Save("test.docx", Aspose.Words.SaveFormat.Docx);
}
else
{
    builder1.Document.Sections[0].HeadersFooters[HeaderFooterType.HeaderPrimary].IsLinkedToPrevious=true;
}
builder1.Document.Save("test.docx", Aspose.Words.SaveFormat.Docx);

@lovecomputer,

I think, any type of header or footer node, that is not present inside a Section, can be considered as linked to the corresponding header or footer of some previous Section. You can build logic on the following C# code of Aspose.Words for .NET API to detect which headers/footers are linked and which are not:

Document doc = new Document("C:\\Temp\\Header.docx");

int index = 1; // should be greater than 0
Section targetSection = doc.Sections[index];

var types = Enum.GetValues(typeof(HeaderFooterType));
foreach (HeaderFooterType headerFooterType in types)
{
    HeaderFooter headerFooter = targetSection.HeadersFooters[headerFooterType];
    if (headerFooter == null)
    {
        Console.WriteLine("{0} is linked", headerFooterType);
    }
    else
    {
        Console.WriteLine("{0} is not linked = {1}", headerFooterType, headerFooter.IsLinkedToPrevious);
        // to link it to previous Section
        headerFooter.IsLinkedToPrevious = true;
    }
}

//// Or To link/unlink alll headers footers
//targetSection.HeadersFooters.LinkToPrevious(false); // or true

And to discard old content and write new content inside a header/footer, please use the following code:

Document doc = new Document("C:\\Temp\\Header.docx");

int index = 0;
Section targetSection = doc.Sections[index];

HeaderFooter primary = targetSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
primary.RemoveAllChildren();

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToSection(index);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

builder.Write("12345");

doc.Save("C:\\Temp\\21.6.docx");

I just want to link the header to the previous section, but not the footers,This code links the header and the footers to the previous section. I only want the header to link to the previous section
headerFooter.IsLinkedToPrevious = true;

@lovecomputer,

You can ignore linking of Footers by adding following condition:

if (headerFooter.HeaderFooterType == HeaderFooterType.HeaderFirst ||
    headerFooter.HeaderFooterType == HeaderFooterType.HeaderPrimary ||
    headerFooter.HeaderFooterType == HeaderFooterType.HeaderEven)
    headerFooter.IsLinkedToPrevious = true;

hh.docx (18.2 KB)
ww.docx (11.9 KB)
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 results
Screenshot.docx (72.0 KB)

@lovecomputer,

Please open ww.docx or hh.docx with MS Word, go to second page, double click the header area, then you will notice that MS Word by default has linked this header to previous (Header & Footer Tools | Navigation | Link to Previous is enabled). Also, the following code returns the same results:

Document doc = new Document("C:\\Temp\\hh.docx");

int index = 1; // should be greater than 0
Section targetSection = doc.Sections[index];

var types = Enum.GetValues(typeof(HeaderFooterType));
foreach (HeaderFooterType headerFooterType in types)
{
    HeaderFooter headerFooter = targetSection.HeadersFooters[headerFooterType];
    if (headerFooter == null)
    {
        Console.WriteLine("{0} is linked", headerFooterType);
    }
    else
    {
        Console.WriteLine("{0} is not linked = {1}", headerFooterType, headerFooter.IsLinkedToPrevious);
    }
}

Also, to determine how many actual headers/footers are there inside a Word document, please use the following code:

Document doc = new Document("C:\\Temp\\hh.docx");
Console.WriteLine("There are total {0} headers/footers in this Word document", doc.GetChildNodes(NodeType.HeaderFooter, true).Count);