Combine Word Documents

I trying to combine Word documents while keeping the header and footer (or lack thereof) intact.
For each document, I am running this code and variations of it but I cannot get the header and footer to not repeat across documents.
I would like to not repeat a documents header and/or footer across any other document.

                using (var w_doc = new WordDocument(stream)
                {
                    foreach (IWSection section in w_doc.Sections)
                    {
                        var hdr = section.HeadersFooters.FirstPageHeader;
                        hdr.ChildEntities.Clear();

                        hdr = section.HeadersFooters.EvenHeader;
                        hdr.ChildEntities.Clear();

                        hdr = section.HeadersFooters.OddHeader;
                        hdr.ChildEntities.Clear();

                        hdr = section.HeadersFooters.EvenFooter;
                        hdr.ChildEntities.Clear();

                        hdr = section.HeadersFooters.OddFooter;
                        hdr.ChildEntities.Clear();

                        hdr = section.HeadersFooters.FirstPageFooter;
                        hdr.ChildEntities.Clear();

                        hdr = section.HeadersFooters.Header;
                        hdr.ChildEntities.Clear();

                        hdr = section.HeadersFooters.Footer;
                        hdr.ChildEntities.Clear();

                        destination.Sections.Add(section.Clone());
                        destination.LastSection.BreakCode = section.BreakCode;
                    }
                }

1 Reply

MR Manikandan Ravichandran Syncfusion Team May 8, 2020 05:39 PM UTC

Hi Steve,

Thank you for contacting Syncfusion support.

From the given details, we have found that your requirement is to combine the Word documents and remove the headers and footers from the resultant document. For this requirement, we have prepared the sample and it can be downloaded from the below link.
https://www.syncfusion.com/downloads/support/forum/154023/ze/F154023889182361

In this sample, we have done the followings,
1. Load the source document and destination documents in different instance.
2. Copy each section from source document and add in the destination document.
3. After added the section into destination document, clear the header and footer contents from the destination document.
4. Save the destination document as new word document.
5. Close the Word document instance.

If we misunderstood any of your requirement, then kindly provide us sample or modified sample of above along with input template, and screenshot/output Word document of the expected result which will helpful to provide you the appropriate solution at the earliest

Please let us know if you have any further questions.

Regards,
Manikandan Ravichandran
 


Loader.
Up arrow icon