|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | + |
| 3 | +namespace Add_bookmarks_to_all_paragraphs_and_retrieve_contents |
| 4 | +{ |
| 5 | + class Program |
| 6 | + { |
| 7 | + public static void Main(string[] args) |
| 8 | + { |
| 9 | + // Load the existing Word document |
| 10 | + WordDocument document = new WordDocument(Path.GetFullPath("Data/Input.docx")); |
| 11 | + // Retrieve all paragraph entities in the document |
| 12 | + List<Entity> paragraphsToInsertBookmarks = document.FindAllItemsByProperty(EntityType.Paragraph, null, null); |
| 13 | + foreach (Entity entity in paragraphsToInsertBookmarks) |
| 14 | + { |
| 15 | + // Cast the entity to a paragraph |
| 16 | + WParagraph currentPara = entity as WParagraph; |
| 17 | + // Skip the paragraph if it is empty |
| 18 | + if (currentPara.Text != string.Empty) |
| 19 | + { |
| 20 | + // Create a unique bookmark name using a GUID |
| 21 | + string bookmarkName = "Bookmark" + Guid.NewGuid(); |
| 22 | + // Insert a bookmark start at the beginning of the paragraph |
| 23 | + currentPara.ChildEntities.Insert(0, new BookmarkStart(document, bookmarkName)); |
| 24 | + // Insert a bookmark end at the end of the paragraph |
| 25 | + currentPara.AppendBookmarkEnd(bookmarkName); |
| 26 | + } |
| 27 | + } |
| 28 | + // Retrieve the contents of all bookmarks in the document |
| 29 | + Dictionary<string, string> bookmarkContents = GetBookmarkContents(document); |
| 30 | + // Print each bookmark name and its corresponding content |
| 31 | + foreach (string bkmkName in bookmarkContents.Keys) |
| 32 | + { |
| 33 | + Console.WriteLine("Corresponding Bookmark : " + bkmkName); |
| 34 | + Console.WriteLine("Content : " + bookmarkContents[bkmkName]); |
| 35 | + } |
| 36 | + Console.ReadLine(); |
| 37 | + } |
| 38 | + /// <summary> |
| 39 | + /// Retrieves all bookmark contents from the document. |
| 40 | + /// </summary> |
| 41 | + /// <param name="document">The Word document containing bookmarks.</param> |
| 42 | + /// <returns>A dictionary with bookmark names as keys and their text content as values.</returns> |
| 43 | + private static Dictionary<string, string> GetBookmarkContents(WordDocument document) |
| 44 | + { |
| 45 | + // Create a dictionary to store bookmark names and their contents |
| 46 | + Dictionary<string, string> bookmarkContents = new Dictionary<string, string>(); |
| 47 | + // Iterate through each bookmark |
| 48 | + foreach (Bookmark currentBookmark in document.Bookmarks) |
| 49 | + { |
| 50 | + // Create navigator to move to the current bookmark |
| 51 | + BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document); |
| 52 | + bookmarkNavigator.MoveToBookmark(currentBookmark.Name); |
| 53 | + // Extract the content inside the bookmark as a temporary Word document |
| 54 | + WordDocument tempDoc = bookmarkNavigator.GetContent().GetAsWordDocument(); |
| 55 | + // Get the text content and add it to the dictionary |
| 56 | + bookmarkContents.Add(currentBookmark.Name, tempDoc.GetText()); |
| 57 | + // Close the temporary document. |
| 58 | + tempDoc.Close(); |
| 59 | + } |
| 60 | + // Return the dictionary containing all bookmark contents |
| 61 | + return bookmarkContents; |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments