Merge documents with different headers and footers

I’m creating a document using the DocumentBuilder class. I want to add a number of documents to the end of my builder and maintain the header and footer settings of the inserted document. I swear this used to work but now my headers and footers are not getting pulled in. Setting CurrentSection.HeadersFooters.LinkToPrevious to true or false doesn’t seem to help.

//load
Document doc_1 = new Document("merge1.docx");
Document doc_2 = new Document("merge2.docx");

//create
Document doc_output = new Document();
DocumentBuilder _builder = new DocumentBuilder(doc_output);

_builder.Writeln("something for the first page");

_builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
_builder.Writeln("something for the header");

_builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
_builder.Writeln("something for the footer");

_builder.MoveToDocumentEnd();

//add section 1
_builder.InsertBreak(BreakType.SectionBreakNewPage);
//_builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
_builder.InsertDocument(doc_1, ImportFormatMode.KeepSourceFormatting);

//add section 2
_builder.InsertBreak(BreakType.SectionBreakNewPage);
//_builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
_builder.InsertDocument(doc_2, ImportFormatMode.KeepSourceFormatting);

//done
doc_output.Save("merged_output.docx", Aspose.Words.SaveFormat.Docx);

merge_docs.zip (23.1 KB)

How can I insert a document using the builder and maintain the header and footers of the inserted document?

@michaelw-2,

Thanks for your inquiry. In your case, we suggest you please use Document.AppendDocument method as shown below. DocumentBuilder.InsertDocument method mimics the MS Word behavior, as if CTRL+‘A’ (select all content) was pressed, then CTRL+‘C’ (copy selected into the buffer) inside one document and then CTRL+‘V’ (insert content from the buffer) inside another document.

Please refer to the following article:
Joining and Appending Documents

Document doc_1 = new Document(MyDir + "merge1.docx");
Document doc_2 = new Document(MyDir + "merge2.docx");

//create
Document doc_output = new Document();
DocumentBuilder _builder = new DocumentBuilder(doc_output);

_builder.Writeln("something for the first page");

_builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
_builder.Writeln("something for the header");

_builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
_builder.Writeln("something for the footer");

_builder.MoveToDocumentEnd();

//add section 1
//_builder.InsertBreak(BreakType.SectionBreakNewPage);
//_builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
//_builder.InsertDocument(doc_1, ImportFormatMode.KeepSourceFormatting);

doc_output.AppendDocument(doc_1, ImportFormatMode.KeepSourceFormatting);

//add section 2
//_builder.InsertBreak(BreakType.SectionBreakNewPage);
//_builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
//_builder.InsertDocument(doc_2, ImportFormatMode.KeepSourceFormatting);
doc_output.AppendDocument(doc_2, ImportFormatMode.KeepSourceFormatting);
//done
doc_output.Save(MyDir + "merged_output.docx", Aspose.Words.SaveFormat.Docx);

Thanks! That worked perfectly.

@michaelw-2,

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