|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +namespace FindTextAddBookmarks |
| 5 | +{ |
| 6 | + class Program |
| 7 | + { |
| 8 | + static void Main(string[] args) |
| 9 | + { |
| 10 | + using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) |
| 11 | + { |
| 12 | + using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic)) |
| 13 | + { |
| 14 | + // Define texts and corresponding bookmark names |
| 15 | + var textBookmarkPairs = new Dictionary<string, string> |
| 16 | + { |
| 17 | + { "they are considered one of the world's most loved animals.", "bkmk1" }, |
| 18 | + { "The table below lists the main characteristics the giant panda shares with bears and red pandas.", "bkmk2" }, |
| 19 | + { "Did you know that the giant panda may actually be a raccoon", "bkmk3" } |
| 20 | + }; |
| 21 | + |
| 22 | + // Add bookmarks to specified texts |
| 23 | + foreach (var pair in textBookmarkPairs) |
| 24 | + { |
| 25 | + AddBookmarkToText(document, pair.Key, pair.Value); |
| 26 | + } |
| 27 | + |
| 28 | + // Retrieve and display bookmark contents |
| 29 | + List<string> bookmarksContent = GetBookmarkContents(document); |
| 30 | + foreach (var content in bookmarksContent) |
| 31 | + { |
| 32 | + Console.WriteLine("Bookmark content: "); |
| 33 | + Console.WriteLine(content); |
| 34 | + } |
| 35 | + |
| 36 | + // Save the modified document |
| 37 | + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 38 | + { |
| 39 | + document.Save(outputStream, FormatType.Docx); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + /// <summary> |
| 45 | + /// Adds a bookmark to a specific text in the document. |
| 46 | + /// </summary> |
| 47 | + private static void AddBookmarkToText(WordDocument document, string searchText, string bookmarkName) |
| 48 | + { |
| 49 | + TextSelection textSelection = document.Find(searchText, false, true); |
| 50 | + if (textSelection != null) |
| 51 | + { |
| 52 | + WTextRange textRange = textSelection.GetAsOneRange(); |
| 53 | + int indexOfText = textRange.OwnerParagraph.Items.IndexOf(textRange); |
| 54 | + textRange.OwnerParagraph.Items.Insert(indexOfText, new BookmarkStart(document, bookmarkName)); |
| 55 | + textRange.OwnerParagraph.Items.Insert(indexOfText + 2, new BookmarkEnd(document, bookmarkName)); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Retrieves all bookmark contents from the document. |
| 61 | + /// </summary> |
| 62 | + private static List<string> GetBookmarkContents(WordDocument document) |
| 63 | + { |
| 64 | + List<string> bookmarkContents = new List<string>(); |
| 65 | + foreach (Entity entity in document.FindAllItemsByProperty(EntityType.BookmarkStart, null, null)) |
| 66 | + { |
| 67 | + if (entity is BookmarkStart bookmarkStart) |
| 68 | + { |
| 69 | + var bookmarkNavigator = new BookmarksNavigator(document); |
| 70 | + bookmarkNavigator.MoveToBookmark(bookmarkStart.Name); |
| 71 | + WordDocumentPart part = bookmarkNavigator.GetContent(); |
| 72 | + WordDocument tempDoc = part.GetAsWordDocument(); |
| 73 | + bookmarkContents.Add(tempDoc.GetText()); |
| 74 | + |
| 75 | + tempDoc.Close(); |
| 76 | + tempDoc.Dispose(); |
| 77 | + part.Close(); |
| 78 | + } |
| 79 | + } |
| 80 | + return bookmarkContents; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments