Bookmarks.Remove does not remove the bookmark using .NET

doc.Range.Bookmarks.remove(bookmarkname) does not work.

also tried clear(bookmarks) which did not help either.

@thomasjn

Please ZIP and attach your input Word document and bookmark name that you want to remove. We will investigate the issue and provide you more information on it.

Hello Tahir, Attached are the requested files and please note that the resulting document from following code still has the original bookmarks defined. Thank you. Lakshmi

                                                            Aspose.Words.License license = new Aspose.Words.License();

                                                            license.SetLicense("Aspose.Total.lic");



                                                            Aspose.Words.Document doc = new Aspose.Words.Document(path + "Mail_Template.docx");



                                                            DocumentBuilder builder = new DocumentBuilder(doc);



                                                            foreach (Bookmark bm in doc.Range.Bookmarks)



                                                            {

                                                                            Aspose.Words.BookmarkStart bookmarkStart = bm.BookmarkStart;

                                                                            Aspose.Words.BookmarkEnd bookmarkEnd = bm.BookmarkEnd;

                                                                            string bmName = bm.Name;

                                                                            string bmText = bm.Text;

                                                                            switch (bmName)

                                                                            {

                                                                                            case "ENTITYNAME":

                                                                                                            bmText = dr["OwnerName"].ToString();

                                                                                                            break;

                                                                                            case "ENTITYADDRESS":

                                                                                                            bmText = dr["Address1"].ToString();

                                                                                                            break;

                                                                                            case "ENTITYCITYSTATEZIP":

                                                                                                            bmText = dr["Address2"].ToString();

                                                                                                            break;

                                                                                            case "TODAYSDATE":

                                                                                                            bmText = DateTime.Today.ToShortDateString();

                                                                                                            break;

                                                                                            case "DUEDATE":

                                                                                                            bmText = dr["Due_Date"].ToString();

                                                                                                            break;

                                                                                            case "LICNUM":

                                                                                                            bmText = dr["licno"].ToString();

                                                                                                            break;

                                                                                            case "TOTALAMOUNT":

                                                                                                            bmText = dr["total"].ToString();

                                                                                                            break;

                                                                                            case "SUBTOTAL":

                                                                                                            bmText = dr["subtotal"].ToString();

                                                                                                            break;

                                                                                            case "RETPMTFEE":

                                                                                                            bmText = dr["retfee"].ToString();

                                                                                                            break;

                                                                            }



                                                                            builder.MoveToBookmark(bmName, true, true);



                                                                            builder.Write(bmText);

                                                                            doc.Range.Bookmarks.Remove(bmName);



                                                            }

                                                            doc.Range.Bookmarks.Clear();



                                                            doc.Save(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "[\\templetters\\](file://templetters/)" + letterid + ".docx");

aspose.zip (68.3 KB)

@thomasjn

We have faced the compile time errors with your code. If you want to remove the bookmarks from the document, please use the following code example.

Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Mail_Template.docx");
doc.Range.Bookmarks.Clear();
doc.Save(MyDir + "20.10.docx"); 

If you want the specific bookmark, please attach the following resources here for testing:

  • Please share the bookmark name.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.