private void CleanTextBetweenBookmark(BookmarkStart bookmark) { var parentOfBookmark = bookmark.Parent; if (parentOfBookmark is Paragraph) { foreach (var text in parentOfBookmark.Descendants<Text>()) { text.Remove(); } } else if (parentOfBookmark is Body) { var bookmarkEnd = parentOfBookmark.Descendants<BookmarkEnd>().FirstOrDefault(x => x.Id.Equals(bookmark.Id)); if (bookmarkEnd != null) { var elementsToRemove = bookmark.ElementsAfter().Intersect(bookmarkEnd.ElementsBefore()).OfType<Text>(); foreach (var text in elementsToRemove) { text.Remove(); } } } }
// cross-element bookmarks are not supported // bookmarks in table cells are not supported internal static IEnumerable <OpenXmlElement> BookmarkContent(BookmarkStart bookmarkStart) { return(bookmarkStart?.ElementsAfter().Where(x => !(x is BookmarkEnd))); }