Click here to Skip to main content
15,896,545 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
// 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 used the above code but Image is not copying from header any help..
Posted

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900