InsertDocument not retaining source header and footer

Hi,

I am merging two documents, I want to retain source document header and footer for that document and rest all pages should maintain destination document header and footer. Below is my code to insert document. It works for inserting document at correct location but is not retaining header and footer of the source document.

var doc = new Document(stream);
doc.FirstSection.PageSetup.RestartPageNumbering = true;
var formatOption = new ImportFormatOptions();
formatOption.IgnoreHeaderFooter = false;
formatOption.KeepSourceNumbering = true;
int sectionIndex = doc.Sections.IndexOf(builder.CurrentSection);
doc.Sections[sectionIndex].PageSetup.SectionStart = SectionStart.NewPage;
doc.Sections[sectionIndex].HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderPrimary, false);
doc.Sections[sectionIndex].HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderEven, false);
doc.Sections[sectionIndex].HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderFirst, false);
doc.Sections[sectionIndex].HeadersFooters.LinkToPrevious(HeaderFooterType.FooterPrimary, false);
doc.Sections[sectionIndex].HeadersFooters.LinkToPrevious(HeaderFooterType.FooterEven, false);
doc.Sections[sectionIndex].HeadersFooters.LinkToPrevious(HeaderFooterType.FooterFirst, false); 

builder.InsertDocument(doc, ImportFormatMode.KeepSourceFormatting, formatOption);

Please help.

@kautilya I would suggest you to use Document.AppenDocument method instead of DocumentBuuder.InsertDocument to merge documents together. Page setup as well as headers/footers in MS Word document are defined per section. If use Document.AppenDocument whole sections from the source documents are copied into the destination documents. While you are using DocumentBuuder.InsertDocument, sections and their settings are not retained. This is an expected behavior.

I used [Document.AppenDocument] but that appends source document at the end of the destination document. My requirement is to add source document dynamically at any location (where user selects) on the destination document.

@kautilya If you need to preserve sections upon inverting documents, you should insert whole sections from the source document. In this case, you can insert a section break at the location where the document should be inserted and then insert sections from the source document between the newly created sections. For example see the following code:

Document dst = new Document(@"C:\Temp\dst.docx");
Document src = new Document(@"C:\Temp\src.docx");

DocumentBuilder builder = new DocumentBuilder(dst);

// Move DocumentBuilder to the location where the document should be inserted.
// For example to bookmark
builder.MoveToBookmark("test");
// Insert a section break.
builder.InsertBreak(BreakType.SectionBreakNewPage);

// Insert sections from the source document before the newly created section.
foreach (Section s in src.Sections)
{
    builder.CurrentSection.ParentNode.InsertBefore(dst.ImportNode(s, true), builder.CurrentSection);
}

dst.Save(@"C:\Temp\out.docx");

Thanks. that worked but introduced new problem. Now it retains header and footer of source document and prints it on all pages of destination document which are coming after completion of source document. Also some time it ruins the formatting of whole document.

builder.StartBookmark(fileDetails.FullName);
builder.MoveToBookmark(fileDetails.FullName);
builder.InsertBreak(BreakType.SectionBreakNewPage);
foreach (Section s in srcDoc.Sections)
{
    builder.CurrentSection.ParentNode.InsertBefore(destDoc.ImportNode(s, true), builder.CurrentSection);
}
builder.EndBookmark(fileDetails.FullName);

image.png (53.9 KB)

in the image above it mixed content of source document and destination document.

@kautilya The following code looks strange:

builder.StartBookmark(fileDetails.FullName);
builder.MoveToBookmark(fileDetails.FullName);

The first line insert the start of the bookmark and the next line moves the document builder cursor to just started bookmark, which is not complete yet. So the second line is redundant in this case.

This occurs because no header/footer are defined in the following sections and headers/footers are inherited from the previous section. You can disable this using HeaderFooterCollection.LinkToPrevious method.

Thanks agian. LinkToPrevious method helps to stop getting header and footers from the source document but it also stops getting original header and footer what destination document had. So, eventually the destination document pages following merged source document are without header and footer. I actually was expecting, it will have the original header and footer from destination document it self.

@kautilya In this case, you should copy headers/footers from the previous section into the newly created section. For example see the following code:

builder.StartBookmark(fileDetails.FullName);
builder.MoveToBookmark(fileDetails.FullName);
builder.InsertBreak(BreakType.SectionBreakNewPage);
// Copy header/footer from the source section into the newly created section.
Section prevSect = (Section)builder.CurrentSection.PreviousSibling;
foreach(HeaderFooter hf in prevSect)
    builder.CurrentSection.HeadersFooters.Add(hf.Clone(true));

foreach (Section s in srcDoc.Sections)
{
    builder.CurrentSection.ParentNode.InsertBefore(destDoc.ImportNode(s, true), builder.CurrentSection);
}

builder.EndBookmark(fileDetails.FullName);

Thanks for all your replies. It is very helpful.

1 Like

why the merged source document adds extra spaces at many of the headings and in headers.

Here, in second image, there is an extra carriage return, which is not present in original source document. Also in first image the heading has extra long spaces which is not there in original source document.

I have made a small change in the code to keep source formatting as below,

builder.StartBookmark(fileDetails.FullName);
builder.InsertBreak(BreakType.SectionBreakContinuous);

foreach (HeaderFooter item in headerFooters)
{
    builder.CurrentSection.HeadersFooters.Add(item.Clone(true));
}
foreach (Section s in srcDoc.Sections)
{
    builder.CurrentSection.ParentNode.InsertBefore(destDoc.ImportNode(s, true, ImportFormatMode.KeepSourceFormatting), builder.CurrentSection);
}
builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
builder.EndBookmark(fileDetails.FullName);

can you please advice on this.

@kautilya Unfortunately, it is difficult to say what is the problem without real documents. Could you please attach sample documents and provide code or application that will allow us to reproduce the problem? We will check the behavior and provide you more information.