|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.IO; |
| 9 | + |
| 10 | +namespace Delete_content_between_two_bookmarks |
| 11 | +{ |
| 12 | + internal class Program |
| 13 | + { |
| 14 | + static void Main(string[] args) |
| 15 | + { |
| 16 | + using (WordDocument document = new WordDocument(@"../../Data/Template.docx", FormatType.Docx)) |
| 17 | + { |
| 18 | + //Delete the text between two different existing bookmarks. |
| 19 | + DeleteBookmarkContent(document, "BM1", "BM2"); |
| 20 | + //Save and close the Word document. |
| 21 | + document.Save(@"../../Data/Result.docx", FormatType.Docx); |
| 22 | + } |
| 23 | + } |
| 24 | + /// <summary> |
| 25 | + /// Deletes the content of a bookmark located between two other bookmarks in the Word document. |
| 26 | + /// A temporary bookmark is created between the two specified bookmarks to identify the content to delete. |
| 27 | + /// </summary> |
| 28 | + /// <param name="document">The <see cref="WordDocument"/> from which the bookmark content will be deleted.</param> |
| 29 | + /// <param name="bookmark1">The name of the first bookmark used to define the starting point of the content to delete.</param> |
| 30 | + /// <param name="bookmark2">The name of the second bookmark used to define the ending point of the content to delete.</param> |
| 31 | + public static void DeleteBookmarkContent(WordDocument document, String bookmark1, String bookmark2) |
| 32 | + { |
| 33 | + //Temp Bookmark. |
| 34 | + String tempBookmarkName = "tempBookmark"; |
| 35 | + |
| 36 | + #region Insert bookmark start after bookmark1. |
| 37 | + //Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name. |
| 38 | + Bookmark firstBookmark = document.Bookmarks.FindByName(bookmark1); |
| 39 | + //Access the bookmark end’s owner paragraph by using bookmark. |
| 40 | + WParagraph firstBookmarkOwnerPara = firstBookmark.BookmarkEnd.OwnerParagraph; |
| 41 | + //Get the index of bookmark end of bookmark1. |
| 42 | + int index = firstBookmarkOwnerPara.Items.IndexOf(firstBookmark.BookmarkEnd); |
| 43 | + //Create and add new bookmark start after bookmark1. |
| 44 | + BookmarkStart newBookmarkStart = new BookmarkStart(document, tempBookmarkName); |
| 45 | + firstBookmarkOwnerPara.ChildEntities.Insert(index + 1, newBookmarkStart); |
| 46 | + #endregion |
| 47 | + |
| 48 | + #region Insert bookmark end before bookmark2. |
| 49 | + //Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name. |
| 50 | + Bookmark secondBookmark = document.Bookmarks.FindByName(bookmark2); |
| 51 | + //Access the bookmark start’s owner paragraph by using bookmark. |
| 52 | + WParagraph secondBookmarkOwnerPara = secondBookmark.BookmarkStart.OwnerParagraph; |
| 53 | + //Get the index of bookmark start of bookmark2. |
| 54 | + index = secondBookmarkOwnerPara.Items.IndexOf(secondBookmark.BookmarkStart); |
| 55 | + //Create and add new bookmark end before bookmark2. |
| 56 | + BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, tempBookmarkName); |
| 57 | + secondBookmarkOwnerPara.ChildEntities.Insert(index, newBookmarkEnd); |
| 58 | + #endregion |
| 59 | + |
| 60 | + #region Select bookmark content and delete. |
| 61 | + //Create the bookmark navigator instance to access the newly created bookmark. |
| 62 | + BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document); |
| 63 | + //Move the virtual cursor to the location of the temp bookmark. |
| 64 | + bookmarkNavigator.MoveToBookmark(tempBookmarkName); |
| 65 | + //Save bookmark content as Word document. |
| 66 | + ExportBookmarkContentToDocument(bookmarkNavigator); |
| 67 | + //Replace the bookmark content. |
| 68 | + bookmarkNavigator.DeleteBookmarkContent(false); |
| 69 | + #endregion |
| 70 | + |
| 71 | + #region Remove that temporary bookmark. |
| 72 | + //Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name. |
| 73 | + Bookmark bookmark = document.Bookmarks.FindByName(tempBookmarkName); |
| 74 | + //Remove the temp bookmark named from Word document. |
| 75 | + document.Bookmarks.Remove(bookmark); |
| 76 | + #endregion |
| 77 | + } |
| 78 | + /// <summary> |
| 79 | + /// Extracts the content of the current bookmark and saves it as a separate Word document (.docx). |
| 80 | + /// </summary> |
| 81 | + /// <param name="bookmarkNavigator">The <see cref="BookmarksNavigator"/> instance used to navigate and access the bookmark content.</param> |
| 82 | + private static void ExportBookmarkContentToDocument(BookmarksNavigator bookmarkNavigator) |
| 83 | + { |
| 84 | + //Get the bookmark content as WordDocumentPart. |
| 85 | + WordDocumentPart documentPart = bookmarkNavigator.GetContent(); |
| 86 | + //Save the WordDocumentPart as separate Word document |
| 87 | + using (WordDocument newDocument = documentPart.GetAsWordDocument()) |
| 88 | + { |
| 89 | + string tempBookmarkName = bookmarkNavigator.CurrentBookmark.Name; |
| 90 | + //Save the Word document to file stream. |
| 91 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath($@"../../Data/{tempBookmarkName}.docx"), FileMode.Create, FileAccess.Write)) |
| 92 | + { |
| 93 | + newDocument.Save(outputFileStream, FormatType.Docx); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments