编程语言
首页 > 编程语言> > c# – 如何从OpenXML docx文件中删除书签?

c# – 如何从OpenXML docx文件中删除书签?

作者:互联网

我认为以下内容可行:

var bod = wordDoc.MainDocumentPart.Document.Body;

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

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

但这会破坏文件.

解决方法:

试试这个,它适用于我的文档.

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();

标签:c,openxml,openxml-sdk
来源: https://codeday.me/bug/20190620/1243098.html