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 Apr 03, 2018 8:08 am

Hello,

I have a word document contains a bookmark. I named it Template.docx and the bookmark name is DistributionHeader.

Image

What I need to do is import the content from another file (Data.docx) to this file and the content should be placed at the bookmark.

Bellow is my code snippet

Code: Select all
var dataFilePath = @"Data.docx";
var templateFilePath = @"Template.docx";
var outputFilePath = @"Output.docx";
var bookmarkName = "DistributionHeader";

//Load documents
var dataDocument = new Document();
dataDocument.LoadFromFile(dataFilePath);

var templateDocument = new Document();
templateDocument.LoadFromFile(templateFilePath);
           
//
//Import content from a document to another document
//Base on this document https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/Document-Operation/Copy-Content-from-one-Word-Document-to-another-in-C-VB.NET.html
//The content will be imported to tempSection
//
Section tempSection = templateDocument.AddSection();
Paragraph paragraph = tempSection.AddParagraph();
foreach (Section section in dataDocument.Sections)
{
    foreach (DocumentObject documentObject in section.Body.ChildObjects)
    {
        tempSection.Body.ChildObjects.Add(documentObject.Clone());
    }
}


//Navigate to the bookmark
var bookmarkNavi = new BookmarksNavigator(templateDocument);
bookmarkNavi.MoveToBookmark(bookmarkName);

//Try to insert every content from the tempSection to the bookmark
foreach (Paragraph paragrapthToInsert in tempSection.Paragraphs)
{
    bookmarkNavi.InsertParagraph(paragrapthToInsert.Clone() as Paragraph);
}

// Remove the unneccessary content
templateDocument.Sections.Remove(tempSection);

templateDocument.SaveToFile(outputFilePath);


If I remove the line
Code: Select all
templateDocument.Sections.Remove(tempSection);


The content from the file Data.docx will display at the end of the file Output.

Otherwise, the Output file will not contain the file Data.docx.

Can someone help me on this.

Thank you very much

voquanghoa
 
Posts: 2
Joined: Tue Apr 03, 2018 7:40 am

Tue Apr 03, 2018 9:51 am

Hello,

Thanks for your post. Please refer to the below code snippet to accomplish your task.
Code: Select all
            var dataFilePath = @"Data.docx";
            var templateFilePath = @"Template.docx";
            var outputFilePath = @"Output.docx";
            var bookmarkName = "DistributionHeader";

            //Load documents
            var dataDocument = new Document();
            dataDocument.LoadFromFile(dataFilePath);

            var templateDocument = new Document();
            templateDocument.LoadFromFile(templateFilePath);

            //Navigate to the bookmark
            var bookmarkNavi = new BookmarksNavigator(templateDocument);
            bookmarkNavi.MoveToBookmark(bookmarkName);

            //insert data
            var objs = dataDocument.Sections[0].Body.ChildObjects;
            for (int i = objs.Count - 1; i >= 0; i--)
            {
                if (objs[i] is Paragraph)
                {
                    bookmarkNavi.InsertParagraph(objs[i].Clone() as Paragraph);
                }
                if (objs[i] is Table)
                {
                    bookmarkNavi.InsertTable(objs[i].Clone() as Table);
                }
            }         
            templateDocument.SaveToFile(outputFilePath);


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Tue Apr 03, 2018 10:35 am

Simon.yang wrote:Hello,

Thanks for your post. Please refer to the below code snippet to accomplish your task.

Best regards,
Simon
E-iceblue support team


Hello Simon,

Your suggestion works.

Thanks for your great help.

voquanghoa
 
Posts: 2
Joined: Tue Apr 03, 2018 7:40 am

Wed Apr 04, 2018 1:40 am

Hello,

Glad to hear that it workes. Welcome to contact us if there is any other question.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.Doc