1

我找不到任何用于替换书签内容的工作代码示例。该代码应该能够处理替换空书签和用预先存在的内容替换书签的情况。

例如:如果我在 Word 文档中有此文本:

“在接下来的时期之间是 Bookmark1.. 在接下来的时期之间是 Bookmark2..”

我想在第一个句点之间插入文本“BM1”,在下一个句点之间插入“BM2”。

第一次更换运行后,更换正确插入。

但是在下一次替换运行后,Bookmark1 之后的行上的所有文本都被删除,然后插入 Bookmark2 的替换。

这是我的 C# 代码:

    var doc = WordprocessingDocument.Open(@"file.docx", true);

    public static Dictionary<string, wd.BookmarkStart> FindAllBookmarksInWordFile(WordprocessingDocument file)
    {
        var bookmarkMap = new Dictionary<String, wd.BookmarkStart>();


        foreach (var headerPart in file.MainDocumentPart.HeaderParts)
        {
            foreach (var bookmarkStart in headerPart.RootElement.Descendants<wd.BookmarkStart>())
            {
                if (!bookmarkStart.Name.ToString().StartsWith("_"))
                    bookmarkMap[bookmarkStart.Name] = bookmarkStart;
            }
        }

        foreach (var bookmarkStart in file.MainDocumentPart.RootElement.Descendants<wd.BookmarkStart>())
        {
            if (!bookmarkStart.Name.ToString().StartsWith("_"))
                bookmarkMap[bookmarkStart.Name] = bookmarkStart;
        }


        return bookmarkMap;
    }
    /*extension methods*/
    public static bool IsEndBookmark(this OpenXmlElement element, BookmarkStart startBookmark)
    {
        return IsEndBookmark(element as BookmarkEnd, startBookmark);
    }

    public static bool IsEndBookmark(this BookmarkEnd endBookmark, BookmarkStart startBookmark)
    {
        if (endBookmark == null)
            return false;

        return endBookmark.Id.Value == startBookmark.Id.Value;
    }
    /* end of extension methods */

    public static void SetText(BookmarkStart bookmark, string value)
    {
        RemoveAllTexts(bookmark);

        bookmark.Parent.InsertAfter(new Run(new Text(value)), bookmark);
    }

    private static void RemoveAllTexts(BookmarkStart bookmark)
    {
        if (bookmark.ColumnFirst != null) return;

        var nextSibling = bookmark.NextSibling();

        while (nextSibling != null)
        {
            if (nextSibling.IsEndBookmark(bookmark) || nextSibling.GetType() == typeof(BookmarkStart))
                break;

            foreach (var item in nextSibling.Descendants<Text>())
            {
                item.Remove();
            }
            nextSibling = nextSibling.NextSibling();
        }
    }

我环顾四周寻找一个通用的解决方案。任何帮助表示赞赏!-胜利者

4

2 回答 2

1

也许这可以帮助你首先:删除书签内容其次:找到书签=>插入值

        public static void InsertTest1(WordprocessingDocument doc, string bookMark, string txt)
            {
                try
                {
                    RemoveBookMarkContent(doc, bookMark);

                    MainDocumentPart mainPart = doc.MainDocumentPart;

                    BookmarkStart bmStart = findBookMarkStart(doc, bookMark);
                    if (bmStart == null)
                    {
                        return;
                    }
                    Run run = new Run(new Text(txt));
                    bmStart.Parent.InsertAfter<Run>(run, bmStart);
                }
                catch (Exception c)
                {
                    //not Exception
                }
            }
    public static void RemoveBookMarkContent(WordprocessingDocument doc, string bmName)
            {
                BookmarkStart bmStart = findBookMarkStart(doc, bmName);
                BookmarkEnd bmEnd = findBookMarkEnd(doc, bmStart.Id);
                while (true)
                {
                    var run = bmStart.NextSibling();
                    if (run == null)
                    {
                        break;
                    }
                    if (run is BookmarkEnd && (BookmarkEnd)run == bmEnd)
                    {
                        break;
                    }

                    run.Remove();
                }
            }
private static BookmarkStart findBookMarkStart(WordprocessingDocument doc, string bmName)
        {
            foreach (var footer in doc.MainDocumentPart.FooterParts)
            {
                foreach (var inst in footer.Footer.Descendants<BookmarkStart>())
                {
                    if (inst.Name == bmName)
                    {
                        return inst;
                    }
                }
            }

            foreach (var header in doc.MainDocumentPart.HeaderParts)
            {
                foreach (var inst in header.Header.Descendants<BookmarkStart>())
                {
                    if (inst.Name == bmName)
                    {
                        return inst;
                    }
                }
            }
            foreach (var inst in doc.MainDocumentPart.RootElement.Descendants<BookmarkStart>())
            {
                if (inst is BookmarkStart)
                {
                    if (inst.Name == bmName)
                    {
                        return inst;
                    }
                }
            }

            return null;
        }
于 2019-06-18T04:08:33.227 回答
0

此代码有效,但当书签放置在字段/表单文本(灰色框)内时无效。

    private static void SetNewContents(wd.BookmarkStart bookmarkStart, string text)
    {
        if (bookmarkStart.ColumnFirst != null) return;

        var itemsToRemove = new List<OpenXmlElement>();

        var nextSibling = bookmarkStart.NextSibling();

        while (nextSibling != null)
        {

            if (IsEndBookmark(nextSibling, bookmarkStart))
                break;

            if (nextSibling is wd.Run)
                itemsToRemove.Add(nextSibling);

            nextSibling = nextSibling.NextSibling();
        }

        foreach (var item in itemsToRemove)
        {
            item.RemoveAllChildren();
            item.Remove();
        }

        bookmarkStart.Parent.InsertAfter(new wd.Run(new wd.Text(text)), bookmarkStart);
    }
于 2013-09-16T10:04:05.830 回答