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.

Tue Jun 21, 2022 11:14 pm

Hello,
how can I copy the content of a document including styles (as a block) and add it to another document as a numeric list?
Thanks for your efforts

Best regards
dirk

Yerodin84
 
Posts: 13
Joined: Thu May 27, 2021 1:39 pm

Wed Jun 22, 2022 8:11 am

Hi dirk,

Thank you for your inquiry.
Please refer to the code below to achieve your requirement. If it does not help you, please provide your input document and desired result for further investigation.
Code: Select all
            //Load source document
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //Create a new document and add a section
            Document newdoc = new Document();
            Section newsec = newdoc.AddSection();
            foreach (Section sec in doc.Sections)
            {
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    //Copy the source document to newdoc
                    newdoc.Sections[0].Body.ChildObjects.Add(obj.Clone());
                }
            }
           
            foreach(DocumentObject obj1 in newsec.Body.ChildObjects)
            {
                if(obj1 is Paragraph)
                {
                    //set numeric list
                    Paragraph para = obj1 as Paragraph;
                    para.ListFormat.ApplyNumberedStyle();
                }
            }
            //Save the newdoc
            newdoc.SaveToFile("result.docx");

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Mon Aug 22, 2022 8:30 am

Hi,

Greetings from E-iceblue.
Has your problem been effectively solved now? Could you give us some feedback at your convenience?

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Return to Spire.Doc