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.

Mon Feb 21, 2022 1:11 pm

The issue is that Bookmarks object from Document object does not contain all the bookmarks that exist in the Word Document. This happens after the method MoveToBookmark is called.
The
Code: Select all
dd1
variable does hold all the bookmarks available in the document and after the foreach loop is executed only some bookmarks are retain in the object. I commented out
Code: Select all
navigator.MoveToBookmark(mapping.SubParagraphCode);
and the issue does not appear anymore, all bookmarks are there, I checked in variable
Code: Select all
dd
. I need the bookmarks to be there later on this is why this is an issue. This is the code:

Code: Select all
BookmarksNavigator navigator = new BookmarksNavigator(document);

         var orderedMappings = mappings
                           .GroupBy(m => m.OptionCode)
                            .OrderBy(m => m.Key)
                           .ToDictionary(x => x.Key, x => x.ToList());

         var a1 = document.Bookmarks;
         List<string> dd1 = new List<string>();
         foreach (var b in a1)
         {
            var ss = b.GetType().GetProperty("Name").GetValue(b, null);
            dd1.Add(ss.ToString());
         }

            foreach (var mappingKey in orderedMappings.Keys)
            {
                if (!modelSelectedOptions.Contains(mappingKey))
                {
                    continue;
                }

                var optionMappings = orderedMappings[mappingKey];

                foreach (var mapping in optionMappings)
                {
                    try
                    {
                        if (null == document.Bookmarks[mapping.SubParagraphCode])
                        {
                            continue;
                        }

                        navigator.MoveToBookmark(mapping.SubParagraphCode);

                        switch (mapping.Action)
                        {
                            case ParagraphChangeAction.MANUAL_ENTRY:
                                {
                                    navigator.ReplaceBookmarkContent(manualEntrieValues[mapping.OptionCode], true);
                                    break;
                                }
                            case ParagraphChangeAction.REPLACE_TEXT:
                                {
                                    navigator.ReplaceBookmarkContent(mapping.ReplaceValue, true);
                                    break;
                                }
                            case ParagraphChangeAction.REMOVE_TEXT:
                                {
                                    navigator.ReplaceBookmarkContent(string.Empty, true);
                                    break;
                                }
                            case ParagraphChangeAction.REMOVE_PARAGRAPH:
                                {
                                    navigator.ReplaceBookmarkContent(string.Empty, true);
                                    break;
                                }
                            case ParagraphChangeAction.KEEP_TEXT:
                            case ParagraphChangeAction.IGNORE_TEXT:
                                {
                                    break;
                                }
                            case ParagraphChangeAction.CALCULATE:
                                {
                                    #region Logic for paragraph P12

                                    if (mapping.SubParagraphCode == "P12_1")
                                    {
                                        navigator.ReplaceBookmarkContent(ParagraphReplacingWords.TakeBy[ParagraphReplacingWords.P12_1][language], true);
                                    }

                                    #endregion

                                    #region Logic for paragraph P15

                                    if (mapping.SubParagraphCode == "P15_3" && modelSelectedOptions.Any(so => so == "H1" || so == "H2"))
                                    {
                                        navigator.ReplaceBookmarkContent(mapping.ReplaceValue, true);
                                    }
                                    if (mapping.SubParagraphCode == "P15_6" && modelSelectedOptions.Any(so => so == "H3"))
                                    {
                                        navigator.ReplaceBookmarkContent(mapping.ReplaceValue.Replace("cu modificarile ulterioare", string.Empty), true);
                                    }

                                    #endregion

                                    #region Logic for paragraph P18

                                    if (mapping.SubParagraphCode == "P18_2" && modelSelectedOptions.Any(so => so == "H1" || so == "H2"))
                                    {
                                        navigator.ReplaceBookmarkContent(mapping.ReplaceValue, true);
                                    }
                                    if (mapping.SubParagraphCode == "P18_5" && modelSelectedOptions.Any(so => so == "H3"))
                                    {
                                        navigator.ReplaceBookmarkContent(mapping.ReplaceValue.Replace("cu modificarile ulterioare", string.Empty), true);
                                    }

                                    #endregion

                                    #region Logic for paragraph P34

                                    if (mapping.SubParagraphCode == "P34_8" && modelSelectedOptions.Any(so => so == "H1" || so == "H2"))
                                    {
                                        navigator.ReplaceBookmarkContent(ParagraphReplacingWords.TakeBy[ParagraphReplacingWords.P34_8_NonH3][language], true);
                                    }
                                    if (mapping.SubParagraphCode == "P34_8" && modelSelectedOptions.Any(so => so == "H3"))
                                    {
                                        navigator.ReplaceBookmarkContent(ParagraphReplacingWords.TakeBy[ParagraphReplacingWords.P34_8_H3][language], true);
                                    }

                                    #endregion

                                    break;
                                }
                            case ParagraphChangeAction.UNCHANGED:
                            case ParagraphChangeAction.TBD:
                            default:
                                {
                                    continue;
                                }
                        }
                    }
                    catch (Exception ex)
                    {
                        var msg = ex.Message;
                        throw;
                    }
                }
            }


            var a = document.Bookmarks;
         List<string> dd = new List<string>();
         foreach (var b in a)
         {
            var ss = b.GetType().GetProperty("Name").GetValue(b, null);
            dd.Add(ss.ToString());
         }

ameridian
 
Posts: 1
Joined: Mon Feb 21, 2022 1:00 pm

Tue Feb 22, 2022 5:38 am

Hello,

Thaks for your inquiry.
I did an initial test but didn't reproduce the issue you mentioned. The version I used is the latest version(Spire.Doc Pack Version:10.1). If you were not using the latest version, please first give it a try. If the issue still exists after trying, please provide the following information for our reference. You could attach them here or send them to us via email (support@e-iceblue.com). Thanks in advance.
1) Your sample Word file.
2) Your complete test project.
3) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese).

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc