Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove bookmarks from an OpenXML docx file?

I thought the following would work:

var bod = wordDoc.MainDocumentPart.Document.Body;

foreach (var bookmark in bod.Descendants<BookmarkStart>())
{
    bookmark.Remove();
}

foreach (var bookmark in bod.Descendants<BookmarkEnd>())
{
    bookmark.Remove();
}

but this corrupts the file.

like image 346
DaveDev Avatar asked Jul 17 '13 15:07

DaveDev


1 Answers

Try this, it worked on my document.

var bs = wordDoc.MainDocumentPart.Document
             .Descendants<BookmarkStart>()
             .ToList();
        foreach (var s in bs)
            s.Remove();

var be = wordDoc.MainDocumentPart.Document
             .Descendants<BookmarkEnd>()
             .ToList();
        foreach (var e in be)
            e.Remove();
like image 62
Ben Scotch Avatar answered Nov 15 '22 09:11

Ben Scotch