簡體   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