Monday, July 29, 2013

How to delete all contents inside a word document bookmark using C# and Open XML

I had the need to mark a certain section in a word document and then remove it by code if it is not needed in a certain scenario. 
My best solution was to use bookmarks, so I had the to remove all contents between two bookmarks (or actually inside one bookmark) inside that word document.

So I marked the section and inserted a bookmarkthat encapsulated it and named it appropriately.

This is the code that did the job:


public void DeleteBetweeen(string bookmarkName)
        {
            var body = m_document.MainDocumentPart.Document.Body;

            //Find the bookmark element, it's either under the root element, or inside a paragraph
            var bookmark = body.Descendants<BookmarkStart>().FirstOrDefault(x => x.Name == bookmarkName);

            //Could not find it.. it maybe inside a more complex element
            if (bookmark == null)
                throw new ArgumentOutOfRangeException("bookmarkName", "Bookmark not found in file");

            //If the bookmark is inside a paragraph we want to delete all siblings of that paragraph
            var start = bookmark.Parent is Paragraph ? bookmark.Parent : bookmark;
            var sibling = start.NextSibling();

            //Delete all elements until we reach our bookmark end tag
            while (!(sibling is BookmarkEnd))
            {
                var temp = sibling;
                sibling = sibling.NextSibling();
                temp.Remove();
            }
            
        }