HeaderFooters had not been merged into the target by using the Method appendDoucment

Hi guy
We planned to merge the document along with the Header and Footer into another document, but found that merging it could create a new section and merge the content of the Body. However, we found that neither the Header nor Footer in the source document were merged.

the code and the corresponding documents as below

Document main = new Document("c:\\temp\\bad\\ok_main.docx");
main.AppendDocument(sub, ImportFormatMode.KeepDifferentStyles, new ImportFormatOptions() { KeepSourceNumbering = true });
main.Save("c:\\temp\\bad\\ok_merged.docx");

ok_main.docx (173.9 KB)

ok_merged.docx (302.9 KB)

sub.docx (215.7 KB)

and below is the expected result

image.png (35.7 KB)

@wengyeung Please try using the following code:

Document main = new Document(@"C:\Temp\ok_main.docx");
Document sub = new Document(@"C:\Temp\sub.docx");
sub.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
main.AppendDocument(sub, ImportFormatMode.KeepDifferentStyles, new ImportFormatOptions() { KeepSourceNumbering = true });
main.Save(@"C:\Temp\out.docx");

It works base on your reference ,topic can be closed ,thx .

1 Like