In most software applications sometimes you need to export a report with the word or any other format and you may also need to merge multiple exported documents into a single document as per requirements.
DocX provides an easy and efficient way to merge two or more existing word documents.
Let's consider we have two documents and we need to merge them into a single word document.
public static void Example1()
{
using (var document1 = DocX.Load(@"D:\one.docx"))
{
// Load the second document.
using (var document2 = DocX.Load(@"D:\two.docx"))
{
// Add a title
var t = document1.InsertParagraph(0, "Merged two Document", false);
t.FontSize(15d);
t.SpacingAfter(50d);
t.Alignment = Alignment.center;
document1.InsertDocument(document2, true);
// Save this document to disk.
document1.SaveAs(@"D:\MergedDocuments.docx");
}
}
}
As you can see in the above example, first it will load one.docx and then load two.docx
The second parameter of InsertDocument()
specifies where to add the document content. When true
, the document is added at the end, and when false
, the document is added at the beginning.
Now when you execute the above example, you will see that the new word document is created that contains the data of both word documents.