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 Feb 22, 2022 1:52 am

I have C# code which locates a bookmark then finds the table inside that bookmark, using:

Code: Select all
BookmarksNavigator navigator = new BookmarksNavigator(spireDoc);
navigator.MoveToBookmark(bkm.Name);
TextBodyPart part = navigator.GetBookmarkContent();
foreach (DocumentObject child in part.BodyItems)
   if (child is Table)
   {
      table =   child as Table;
      break;
   }


This gives me a valid reference to a table object, and when I remove a row, I see in the debugger the row gets removed from the rows collection in the table, but when the final document is printed, all of the rows are still there.

I added to this code the command before removing the row: tableRow.Cells.Clear()
After this, the cells collection shows Count = 0, but when the document prints, all of the cells are still present in the row.

I have tried both table.Rows.RemoveAt(1) and using a reference to the row, table.Rows.Remove(tableRow)
The table object changes in the debugger but the document does not get updated.
Am I using the table object incorrectly?

(using free Spire.Doc to set up a suitable demonstration to request purchasing the product)

Note: My source is a Word .dot template, which I update at various bookmarks, then save the final document as a PDF.
Last edited by Les43625 on Wed Feb 23, 2022 3:28 am, edited 1 time in total.

Les43625
 
Posts: 12
Joined: Tue Feb 22, 2022 1:41 am

Tue Feb 22, 2022 3:48 am

Hello,

Thanks for your inquiry!

I tested to remove rows from table with the following code, but not reproduce your issue. The row can be deleted well. Please refer to the code I provided on your side, if the issue still exists, please provide us with the input Word file for further investigate. Thanks in advance!

Code: Select all
            Document document = new Document();
            document.LoadFromFile(@"E:\testdoc\doc2.docx");
            BookmarksNavigator navigator = new BookmarksNavigator(document);
            navigator.MoveToBookmark("table");
            Table table = new Table(document);
            TextBodyPart part = navigator.GetBookmarkContent();
            foreach (DocumentObject child in part.BodyItems)
                if (child is Table)
                {
                    table = child as Table;
                    break;
                }

            table.Rows.RemoveAt(1);
            //apply the change
            navigator.ReplaceBookmarkContent(part);
            document.SaveToFile("res.docx");


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Tue Feb 22, 2022 4:08 am

Thank you! I was thinking that the table object was a reference to the document content. The method call navigator.ReplaceBookmarkContent(part) was what I was missing.

Les43625
 
Posts: 12
Joined: Tue Feb 22, 2022 1:41 am

Tue Feb 22, 2022 5:53 am

Hello,

You are welcome!

If you encounter any issues related to our product in the future, just feel free to contact us.

Have a nice day!

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.Doc