We have two Docx files, one having the header templates(Letter Head) with Header and Footer parts and the title and the second file have the body. We need to combine both the Docx files into a single Docx File. Is it possible to copy all the sections and body parts of docx files into a new document.
We have tried the following code but we are getting exception on the second document App Parts Method:
Only one instance of the type is allowed for this parent.
using (WordprocessingDocument firstDocument = WordprocessingDocument.Open(fileName, false))
using (WordprocessingDocument document = WordprocessingDocument.Open(source1, false))
using (WordprocessingDocument secondDocument = WordprocessingDocument.Create(destination, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = secondDocument.MainDocumentPart;
foreach (var part in firstDocument.Parts)
{
secondDocument.AddPart(part.OpenXmlPart, part.RelationshipId);
}
foreach (var part in document.Parts)
{
secondDocument.AddPart(part.OpenXmlPart, part.RelationshipId);
}
}