How to read/write to header//body/footer only?

I want to read all the content controls in a document and I need to know which ones are in a header or footer and which are just in the body of text. Is there some way to read just the header, body or the footer rather than the whole document or is there some property embedded in the content controls (presumably in the parent nodes)?

Also I need to insert an image and text into a footer (QR Code and version number). Please can anyone advise what objects I should be using to do this?

I’m working in C#. Thanks for reading!

@SCDGLC

Please use Node.GetAncestor method as shown below to check either a node is child node of Body node or HeaderFooter.

if (node.GetAncestor(NodeType.Body) != null)
{
    //node is in body of document
}
else
{
    //node is in the header/footer of document
}

Please move the cursor to the header/footer of document and insert the content. We suggest you please read the following articles.
Moving the Cursor
Use DocumentBuilder to Insert Document Elements

1 Like

That’s fantastic, thanks so much, I will check out those articles! However how can I decipher as to whether a node is in a footer or a header because the nodetype has headerfooter as a combined property?

@SCDGLC

Please use the following code snippet to check parent node type either it is header of footer.

if (node.GetAncestor(NodeType.HeaderFooter) != null)
{
    HeaderFooter headerFooter = (HeaderFooter)node.GetAncestor(NodeType.HeaderFooter);
    if (headerFooter.HeaderFooterType == HeaderFooterType.HeaderPrimary)
    {

    }
    else if (headerFooter.HeaderFooterType == HeaderFooterType.FooterPrimary)
    {

    }
}
1 Like

Thanks tahir.manzoor, much appreciated!

It appears to work just fine, however it is missing loads of the content controls. Some of the ones in the footers and headers are repeated, however some of the repeated ones are found and others are not. Do you know why it’s not returning all of them? I would expect it to either return all instances of content controls in headers/footers or only one instance but it’s returning a few but not all. This is my code…

        Document doc = new Document(filenameAndPath);

        foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
        {
            if (sdt.GetAncestor(NodeType.HeaderFooter) != null)
            {
                HeaderFooter headerFooter = (HeaderFooter)sdt.GetAncestor(NodeType.HeaderFooter);
                if (headerFooter.HeaderFooterType == HeaderFooterType.HeaderPrimary)
                {
                    // ...in header
                    Console.WriteLine("HEADER: " + sdt.Tag + "_" + sdt.Title);
                }
                else if (headerFooter.HeaderFooterType == HeaderFooterType.FooterPrimary)
                {
                    // ...in footer
                    Console.WriteLine("FOOTER: " + sdt.Tag + "_" + sdt.Title);
                }
            }
            else
            {
                // ...in body
                Console.WriteLine("BODY: " + sdt.Tag + "_" + sdt.Title);
            }
        }

PS: I need to amend/delete content controls in tables. Do I need to handle them differently or can I just amend/delete them in the same way I handle any other content controls?

@SCDGLC

We suggest you please read the following article.
Aspose.Words Document Object Model

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

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • 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 will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

1 Like

Thanks for the link, that has enabled me to work out how to insert the image at the start of the table using builder.MoveToCell(0, 0, 0, 0) after having moved to the footer. :smile:

However I am still confused as to why some duplicate content controls in headers and footers are included in my results and others are missing. Could it have anything to do with the fact that the word numbering for the document is 1-21 and then 1-3 and then 1 of 1 and another 1 of 1. It’s because of historical reasons that the page numbering is messed up, would that make a difference?

I’m guessing it might because moving the cursor to the last paragraph moves it to the end of page 21 of 21 pages but ignores the 3 pages plus the 2 single pages that come after that. What is odd though is that it’s not showing all the content controls in the headers and footers for the first 21 pages but only for a few pages…unless it’s only showing results for the set of 3 pages (1 of 3, 2 of 3 and 3 of 3 instead of the first 21 pages) but not sure why it would jump to the middle of the document as I didn’t code it to do that?

@SCDGLC

Unfortunately, it is difficult to say what the problem is without documents and code example. Please share requested detail here for testing. Thanks for your cooperation.

1 Like

Ok I’ve created a test file to upload…(you already have the test code above that I’m running).

Thanks for your help!

Test File.zip (131.2 KB)

FYI it seems to me that the code is returning one instance of each of the content controls in the header per set of pages in the document (in this case there are 2 content controls in each header and 3 sets of pages - 1-13, 1-3, 1 and 1 so it returns 8 content controls in the header).
However oddly it only returns one instance of the content control in the footer and going by how it appears to handle content controls in the headers, I would have expected to return 4 instances of it.
All that said, I need it to either return all instances or just one instance in any given header or footer, no matter how many sets of pages in a document so I know how to handle them.
Interestingly it returns the expected number of content controls in the body, even if I copy some to different sets of pages within the document.
Hope that makes sense!

@SCDGLC

We are working over your query and will get back to you soon.

1 Like

@SCDGLC

You are facing the expected behavior of Aspose.Words. The HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter or each HeaderFooterType in a Section.

If Section does not have a HeaderFooter of a specific type or the HeaderFooter has no child nodes, this header/footer is considered linked to the header/footer of the same type of the previous section in Microsoft Word.

A section can or cannot have one or multiple pages in Word document. Please use the following code example to get the content control in header/footer and body of each section in the document.

Document doc = new Document(MyDir + "Test File.docx");

foreach (Section section in doc.Sections)
{
    foreach (HeaderFooter headerFooter in section.HeadersFooters)
    {
        foreach (StructuredDocumentTag sdt in headerFooter.GetChildNodes(NodeType.StructuredDocumentTag, true))
        {
            if (headerFooter.HeaderFooterType == HeaderFooterType.HeaderPrimary)
            {
                Console.WriteLine("HeaderPrimary : " + sdt.Tag + "_" + sdt.Title);
            }
            else if (headerFooter.HeaderFooterType == HeaderFooterType.FooterPrimary)
            {
                Console.WriteLine("FooterPrimary : " + sdt.Tag + "_" + sdt.Title);
            }                            
        }

        foreach (StructuredDocumentTag sdt in section.Body.GetChildNodes(NodeType.StructuredDocumentTag, true))
            Console.WriteLine("BODY: " + sdt.Tag + "_" + sdt.Title);
    }

    Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
1 Like

Brilliant - thanks so much!! <3