Using AppendContent with Headers and Footers


#1

I have 3 documents that I am working with, one document contains the header that I want to use. The second contains the footer that I want to use. The third document contains the body content I want to use. I try to use the AppendContent to append the header, then the footer and then the body. The output contains the header and the body, but the footer does not appear.

If I put the header and footer content into the same document, it creates the correct output, however this solution is not ideal.

Is there anything that I can do about this?

Thanks,

John


#2

Main text (we call it Body) and each header/footer in a Word document is considered a separate text story. Technically speaking a story is a container for block-level elements (paragraphs and tables).

Section.AppendContent and PrependContent are shortcut methods to deal with the main text story only and they don't copy the headers, footers and page setup of the section.

To do what you want, you need to use the latest Aspose.Word 3.0 that exposes all document content as nodes in a tree like structure. We are going to add more documentation for it including diagrams, but to get your started:

Section can contain contain
Body (maximum one)
HeaderFooter (maximum one of each HeaderFooterType)

See Section, Body, HeaderFooter and HeadersFooters classes in the API, check their methods, if you ever uses XmlDocument, you should be familiar with AppendChild, InsertBefore, InsertAfter and so on. You just need to move the nodes the way you want it.

By the way, if you want to move a node from one document to another, you need to use Document.ImportNode first (again similar to XmlDocument), it makes a copy of the original node and adjusts it so it will be valid in the destination document.


#3

Thanks Roman, I was able to get that to work. However I have another question along the same lines:

Is there a method to insert a header as a First Page Header/Footer, if the document has different first page headers and footers?


#4

jkoerner wrote:
Thanks Roman, I was able to get that to work. However I have another question along the same lines:

Is there a method to insert a header as a First Page Header/Footer, if the document has different first page headers and footers?


Nevermind... I figured it out.

Here is how I did it if anyone cares:

Dim objHF As New Aspose.Word.HeaderFooter(myNewDoc, Aspose.Word.HeaderFooterType.HeaderFirst)

Dim objCurrHeader As Aspose.Word.HeaderFooter objCurrHeader = myNewDoc.Sections(0).HeadersFooters(Aspose.Word.HeaderFooterType.HeaderPrimary)

For i = 0 To objCurrHeader.ChildNodes.Count - 1
objHF.AppendChild(objCurrHeader.ChildNodes(i).Clone(True))
Next

myNewDoc.Sections(0).PageSetup.DifferentFirstPageHeaderFooter = True
myNewDoc.Sections(0).HeadersFooters.Add(objHF)


#5

To insert different first or odd/even pages header footers you need to do two things: append a HeaderFooter of the appropriate type to a Section (or insert a header footer using DocumentBuilder) and also specify in Section.PageSetup to use different headers footers.

If you want to create a HeaderFooter directly as a node do this:

Section mySection = doc.Sections[0]; //Say you want to add to the 1st section.

//Check if the header/footer of this type already there because cannot add it twice.
HeaderFooter firstHdr = mySection.HeadersFooters[HeaderFooterType.HeaderFirst];

if (firstHdr == null)
{
firstHdr = new HeaderFooter(doc, HeaderFooterType.HeaderFirst);
mySection.HeadersFooters.Add(firstHdr);
}

//Indicate that MS Word should use the different first page header footer.
mySection.PageSetup.DifferentFirstPageHeaderFooter = truel

//Say we want to clear the header
firstHdr.RemoveAllChildren();

//Add an empty paragraph.
Paragraph para = new Paragraph(doc);
firstHdr.AppendChild(para);

//Add some text to the paragraph
para.AppendChild(new Run(doc, “Text in the first page header.”));

----------

Related links:
http://www.aspose.com/Products/Aspose.Word/Api/Aspose.Word.PageSetup.DifferentFirstPageHeaderFooter.html
http://www.aspose.com/Products/Aspose.Word/Api/Aspose.Word.PageSetup.OddAndEvenPagesHeaderFooter.html

The above links have an example how to insert text into header footer using DocumentBuilder.