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 17, 2018 9:06 am

Hello,
we have MS-Word templates with tables (existing Headers and one empty row with an bookmark in the first column) which we have to fill with data.
How to get the table via the bookmark and replace the existing data with new one?

thx for helping me
Johannes

gaj
 
Posts: 6
Joined: Thu Apr 12, 2018 8:48 am

Tue Apr 17, 2018 10:05 am

Hello Johannes,

Thanks for your inquiry. Please refer to the below code snippet to accomplish your task.
Code: Select all
            Document document = new Document();
            document.LoadFromFile(@"test.docx");

            //create BookmarksNavigator instance
            BookmarksNavigator bn = new BookmarksNavigator(document);
            //find the bookmark which name is test
            bn.MoveToBookmark("test", true, true);

            //get the table
            var table = bn.CurrentBookmark.BookmarkStart.Owner.Owner.Owner.Owner;
            if (table is Table)
            {
                Table bookmarkTable = table as Table;
                //add data
                bookmarkTable.Rows[0].Cells[0].Paragraphs[0].Text = "hello";
            }

            //save
            string output = "result.docx";
            document.SaveToFile(output, FileFormat.Docx);
            System.Diagnostics.Process.Start(output);


Best regards,
Simon
E-iceblue support team
User avatar

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

Tue Apr 17, 2018 1:58 pm

thx. works fine
Johannes

gaj
 
Posts: 6
Joined: Thu Apr 12, 2018 8:48 am

Wed Apr 18, 2018 1:29 am

Hello,

Glad to hear that it worked. Just feel free to contact us if you have questions.

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

cron