繁体   English   中英

OpenXML SDK C#:如何将书签内容从 1 个 word 文件复制到另一个 word 文件

[英]OpenXML SDK C#: How to Copy Bookmark content from 1 word file to another word file

我需要在 C# 中使用 OpenXMl SDK 创建 word 文件,我有2-word files我需要将一些书签内容从1st-word file复制到2nd-word file并保存文件。

提前致谢。

我尝试了不同的方式,下面是我的代码。 我收到错误消息,因为“无法插入 OpenXmlElement‘newChild’,因为它是树的一部分。”

public void testingExecute()
    {

        try
        {
            using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1,false))
            using (WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true))
            {

                BookmarkStart bookmarkStart = new BookmarkStart();
               
                var res = from bm in wordDoc1.MainDocumentPart.RootElement.Descendants<BookmarkStart>()
                          where bm.Name == "TestBookmark"
                          select bm;
                
                var bookmarkstart = res.SingleOrDefault();
                var res1 = from bm in wordDoc1.MainDocumentPart.RootElement.Descendants<BookmarkEnd>()
                           where bm.Id == bookmarkstart.Id
                           select bm;
                var bookmarkend = res1.SingleOrDefault();

                body.InsertBeforeSelf<BookmarkStart>(bookmarkstart);
                body.InsertAfterSelf<BookmarkEnd>(bookmarkend);

                
                wordDoc2.MainDocumentPart.Document.Save();

        }
        catch (Exception Ex)
        {

            throw;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM