Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Oct 10, 2016 10:44 am

Hi,

We use the Clone() function to merge several documents to one. The clone() functions always adds a section break, (why???)
to finalize the document we need to do is replace all the section break with line feeds. Is there a way to do this?

regs
Oliver

HMP
 
Posts: 6
Joined: Tue Aug 10, 2010 10:19 am

Tue Oct 11, 2016 2:13 am

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,
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Oct 13, 2016 9:18 am

Dear Oliver,

Did you test the solution I provided ? And has your issue been resolved ?
Looking forward to your reply.

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Oct 18, 2016 10:52 am

Yes, your Code fixed our problem.

HMP
 
Posts: 6
Joined: Tue Aug 10, 2010 10:19 am

Wed Oct 19, 2016 1:19 am

Dear Oliver,

Thanks for your feedback.
Please feel free to contact us if there is any question. We will be happy to help you.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc