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.

Sat Feb 13, 2016 12:46 am

I am trying to insert document B at a specific bookmark point in document A. I can do this if document B only contains paragraphs of text by doing:
foreach (Paragraph myPara in section0.Paragraphs)
{
Paragraph tempPara = (Paragraph)myPara.Clone();
bn.InsertParagraph(tempPara);

}
But how can I do this if document B contains tables and other items? Thank you.

mfoitzik
 
Posts: 2
Joined: Thu Feb 11, 2016 6:21 pm

Sun Feb 14, 2016 6:08 am

Hi,

Thanks for your posting and using our component.
Please try the following solution.
Code: Select all
       
            Document docA = new Document();
            docA.LoadFromFile(fileA);
            Document docB = new Document();
            docB.LoadFromFile(fileB);
            Bookmark bookmark1 = docA.Bookmarks["AddNewDocument"];
       
            Paragraph bookPra = bookmark1.BookmarkStart.OwnerParagraph;
           
                Body sourceBody = bookPra.OwnerTextBody as Body;
                int indexPara = sourceBody.ChildObjects.IndexOf(bookPra);

                foreach (Section section in docB.Sections)
                {
                    for (int i = 0; i < section.Body.ChildObjects.Count; i++)
                    {
                        sourceSection.ChildObjects.Insert(indexPara++, section.Body.ChildObjects[i].Clone());
                    }
                }
            string output = "result.docx";
            docA.SaveToFile(output,FileFormat.Docx2010);


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Feb 15, 2016 8:00 am

Hi,

Did my solution resolve your issue?
Thank you for your feedback.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Tue Feb 16, 2016 7:28 pm

Hello Amy,

Yes it worked. You are awesome! The only correction I had to make is change sourceSection to sourceBody inside the if loop.

Mike

mfoitzik
 
Posts: 2
Joined: Thu Feb 11, 2016 6:21 pm

Wed Feb 17, 2016 1:31 am

Hi Mike,

Thanks for your feedback.
I am glad that your issue has been resolved. Welcome to feel free to write to us for further problems.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc