Dear Oliver,
Thanks for your inquiry.
There are two solutions to merge documents into one section.
Solution#1, collect the all objects in those documents and then create a new target document, and then add objects into the target document.
- Code: Select all
Document OriginalDoc1 = new Document();
OriginalDoc1.LoadFromFile(@"8790 Original.docx");
Document OriginalDoc2 = new Document();
OriginalDoc2.LoadFromFile(@"8790 Main.docx");
List<DocumentObject> elements = new List<DocumentObject>();
foreach (Section Orisec1 in OriginalDoc1.Sections)
{
foreach (DocumentObject element in Orisec1.Body.ChildObjects)
{
elements.Add(element);
}
}
foreach (Section Orisec2 in OriginalDoc2.Sections)
{
foreach (DocumentObject element in Orisec2.Body.ChildObjects)
{
elements.Add(element);
}
}
Document TargetDoc = new Document();
Section sec = TargetDoc.AddSection();
foreach (DocumentObject element in elements)
{
sec.Body.ChildObjects.Add(element.Clone());
}
TargetDoc.SaveToFile("MergeSection8790.docx", FileFormat.Docx);
Solution#2, set one document as target document and then clone other document's content into its body. Please note that the result document will keep same format as the target document.
- Code: Select all
Document OriginalDoc1 = new Document();
OriginalDoc1.LoadFromFile(@"8790 Original.docx");
Document TargetDoc = new Document();
TargetDoc.LoadFromFile(@"8790 Main.docx");
List<DocumentObject> elements = new List<DocumentObject>();
foreach (Section Orisec1 in OriginalDoc1.Sections)
{
foreach (DocumentObject element in Orisec1.Body.ChildObjects)
{
elements.Add(element);
}
}
foreach (DocumentObject element in elements)
{
TargetDoc.Sections[0].Body.ChildObjects.Add(element.Clone());
}
TargetDoc.SaveToFile("8790MergeSection.docx", FileFormat.Docx);
If there is any question, welcome to get it back to us.
Sincerely,
Betsy
E-iceblue support tea,