How to merge multiple documents with header and footer of destination document?
Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can merge multiple documents with header and footer of destination document using C#.
Steps to merge multiple documents with header and footer of destination document
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
- Use the following code example to merge multiple documents with header and footer of destination document using C#.
C#
//Open the destination document as a stream. using (FileStream destinationStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //Open the destination document. using (WordDocument destinationDocument = new WordDocument(destinationStreamPath, FormatType.Automatic)) { //Get the Source document names from the folder. string[] sourceDocumentNames = Directory.GetFiles(@"../../../Data/SourceDocuments/"); //Merge each source document to the destination document. foreach (string subDocumentName in sourceDocumentNames) { //Open the source document files as a stream. using (FileStream sourceDocumentPathStream = new FileStream(Path.GetFullPath(subDocumentName), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //Open the source documents. using (WordDocument sourceDocuments = new WordDocument(sourceDocumentPathStream, FormatType.Docx)) { //Iterate source document sections. foreach (WSection sourceDocumentSections in sourceDocuments.Sections) { //Clear the headers and footers of the source documents. sourceDocumentSections.HeadersFooters.FirstPageHeader.ChildEntities.Clear(); sourceDocumentSections.HeadersFooters.FirstPageFooter.ChildEntities.Clear(); sourceDocumentSections.HeadersFooters.EvenHeader.ChildEntities.Clear(); sourceDocumentSections.HeadersFooters.EvenFooter.ChildEntities.Clear(); sourceDocumentSections.HeadersFooters.OddHeader.ChildEntities.Clear(); sourceDocumentSections.HeadersFooters.OddFooter.ChildEntities.Clear(); } //Import source documents to destination document. destinationDocument.ImportContent(sourceDocuments); } } } //Create a file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite)) { //Save the Word document to the file stream. destinationDocument.Save(outputFileStream, FormatType.Docx); } } }
A complete working sample to merge multiple documents with header and footer of destination document using C# can be downloaded from GitHub.
Input destination Word document as follows.
Input source Word documents as follows.
By executing the program, you will get the output document as follows.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion Word Framework features.
How to merge multiple Word documents in C#, VB.NET
How to merge one document to another with the margins of destination document?
Conclusion
I hope you enjoyed learning about how to merge multiple documents with header and footer of destination document.
You can refer to our ASP.NET Core Word Library’s feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET Core Word Library documentation to understand how to present and manipulate data.
For current customers, you can check out our ASP.NET Core components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our ASP.NET Core Word Library and other ASP.NET Core components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!