Free Support Forum - aspose.com

Detect if any header / footer is defined in a document

Hi!


Our application merge multiple source documents (.doc) and output the result as one big document (.doc or .pdf).

When merging documents, we have to check if actual document has header or footer. If not, we apply the last header / footer with linktoprevious = true. If the document has header / footer, we set linktoprevious = false.

For example:

Doc1 > Header Footer Defined
Doc2 > No header / footer
Doc3 > No header / footer
Doc4 > Header Footer Defined

The final result would be:

FinalDoc {
Doc1 > Keeps header footer
Doc2 > Link to previous
Doc3 > Link to previous
Doc4 > No link to previous / keeps header footer
}

To apply the “good” logic, I have to check if actual document has header or footer defined. To do so, I call this method with each 6 header / footer types:

Shared Function HeaderFooterDefined(ByVal headerFooter As HeaderFooter) As Boolean
Return headerFooter IsNot Nothing AndAlso headerFooter.ChildNodes.Count > 0
End Function

The problem is that I have a document in which one header / footer was defined. I tried to clear them using MS Word functions “Delete header” and “Delete footer”. I also tried to clear them by using CTRL+A / Delete, CTRL+A / Backspace, ETC.

When merging the doc and calling the above function, it detects that primary header / primary footer has 1 child node. A paragraph with no child, no runs.

So my question is:
Is there a better way to detect if there is a header or footer defined in a document or is there a way to clear them definitely from MS WORD?

I hope to be clear enough!
Have a nice day.

Regards,

JSr

Hi,


Thanks for your inquiry. Please use the HeaderFooterCollection.Clear method to remove header/footer from documents. This method removes all nodes from HeaderFooterCollection collection. Please use the following code snippet for your kind reference.


<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:#2B91AF;mso-font-kerning:
0pt;mso-no-proof:yes”>Document<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> doc = new Document(MyDir

  • “in.docx”);<o:p></o:p>

foreach (Section section in doc.Sections)

{

if (section.HeadersFooters.Count > 0)

section.HeadersFooters.Clear();

}

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


I suggest you please read following documentation links for your kind reference. Hope this answers your query. Please let us know if you have any more queries.
http://www.aspose.com/docs/display/wordsnet/Section+Class
http://www.aspose.com/docs/display/wordsnet/Sections+Overview
http://www.aspose.com/docs/display/wordsnet/HeaderFooterCollection+Class

Hi M. Manzoor!


Thank you once again for the quick answer! I may have been, unfortunately, unclear… The code you’ve provided would work, of course, if I would like to clear headers / footers. BUT, what I’m trying to realize is to DETECT if there are any header / footer in the current document.

I’ve attached a simple example to demonstrate the problem. The first document has header / footer and the second one DON’T have any header / footer. As you can see in the code, I try to detect if the document has any header / footer defined and IF NOT (as i should expect from the second document), we LinkToPrevious(true) do the header / footer of the first document continues on the second.

The problem is that the second document has as filled headerfooter collection with 1 childnode (a paragraph) in each headerfootertype (primary, first, even).

So how to detect properly if there are header footer in the document or how to totally clear header / footer from WORD so the headerfooter collection would be empty or without any childnodes.

Thank you very much and have a nice day,

JSr
Hi,

Thanks for your inquiry.
jeezypop:
BUT, what I'm trying to realize is to DETECT if there are any header / footer in the current document.

So how to detect properly if there are header footer in the document
You can use HeaderFooterCollection.Count to gets the number of nodes in the collection.
jeezypop:
I've attached a simple example to demonstrate the problem. The first document has header / footer and the second one DON'T have any header / footer. As you can see in the code, I try to detect if the document has any header / footer defined and IF NOT (as i should expect from the second document), we LinkToPrevious(true) do the header / footer of the first document continues on the second.

FinalDoc {
Doc1 > Keeps header footer
Doc2 > Link to previous
Doc3 > Link to previous
Doc4 > No link to previous / keeps header footer
}
Your first document (firstDoc.docx) has header/footer with text (This is the header/This is the footer).
Second document (secondDoc.docx) has no header/footer.
Third document (Infos.docx) has header/footer with empty paragraphs.

In your case, you need to check two thing. 1) The document has no header and footer 2) The last appended document's header/footer are linked to previous (IsLinkedToPrevious = true).

If both cases are true then set LinkToPrevious to true as shown in following code snippet. Hope this answers your query. Please let us know if you have any more queries.

Dim doc As New Document(dataDir & "firstDoc.docx")

Dim doc2 As New Document(dataDir & "secondDoc.docx")

Dim doc3 As New Document(dataDir & "infos.docx")

' Apply header footer logic on both documents

ApplyHeaderFooterLogic(doc2, doc)

' Merge documents

doc.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles)

ApplyHeaderFooterLogic(doc3, doc)

doc.AppendDocument(doc3, ImportFormatMode.UseDestinationStyles)

' Save the output to file

doc.Save(dataDir & "HeaderFooterTest.docx", SaveFormat.Docx)

Private Shared Sub ApplyHeaderFooterLogic(ByVal doc As Document, ByVal doc_Previous As Document)

' Scan each section and, if no header / footer defined, set linktoprevious to TRUE

For Each sec As Section In doc.Sections

If Not Utils.HeadersFootersDefined(sec) Then

sec.HeadersFooters.LinkToPrevious(True)

ElseIf doc_Previous.LastSection.HeadersFooters(0).IsLinkedToPrevious = True Then

sec.HeadersFooters.LinkToPrevious(True)

End If

Next

End Sub

Sorry for the late answer…! I will give your solution a try as soon as I can and give you feedback!


Thanks once again,

Jr

Hi Jr,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.