如何将一个.docx文件的头文件替换为其他.docx文件格式c#

问题描述:

// Replace header in target document with header of source document.
           using (WordprocessingDocument
               wdDoc = WordprocessingDocument.Open(filepathTo, true))
           {
               MainDocumentPart mainPart = wdDoc.MainDocumentPart;

               // Delete the existing header part.
               mainPart.DeleteParts(mainPart.HeaderParts);

               // Create a new header part.
               DocumentFormat.OpenXml.Packaging.HeaderPart headerPart =
           mainPart.AddNewPart<HeaderPart>();

               // Get Id of the headerPart.
               string rId = mainPart.GetIdOfPart(headerPart);

               // Feed target headerPart with source headerPart.
               using (WordprocessingDocument wdDocSource =
                   WordprocessingDocument.Open(filepathFrom, true))
               {
                   DocumentFormat.OpenXml.Packaging.HeaderPart firstHeader =
           wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault();

                   wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault();

                   if (firstHeader != null)
                   {
                       headerPart.FeedData(firstHeader.GetStream());
                   }
               }

               // Get SectionProperties and Replace HeaderReference with new Id.
               IEnumerable<DocumentFormat.OpenXml.Wordprocessing.SectionProperties> sectPrs =
           mainPart.Document.Body.Elements<SectionProperties>();
               foreach (var sectPr in sectPrs)
               {
                   // Delete existing references to headers.
                   sectPr.RemoveAllChildren<HeaderReference>();

                   // Create the new header reference node.
                   sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId });
               }
           }
       }



i使用了上面的代码,但是Image没有从头文件中复制任何帮助..


i used the above code but Image is not copying from header any help..

看到:

http://www.c-sharpcorner.com/UploadFile/Globalking/fileAccessingusingcsharp02242006050207AM/fileAccessingusingcsharp.aspx [ ^ ]

之后可能需要编写更多代码。
See this:
http://www.c-sharpcorner.com/UploadFile/Globalking/fileAccessingusingcsharp02242006050207AM/fileAccessingusingcsharp.aspx[^]
May be you have to write more code afterwards.