public static void AddHeaderFromTo(string filepathFrom, string filepathTo)
{
using (WordprocessingDocument
wdDoc = WordprocessingDocument.Open(filepathTo, true))
{
MainDocumentPart mainPart = wdDoc.MainDocumentPart;
mainPart.DeleteParts(mainPart.HeaderParts);
DocumentFormat.OpenXml.Packaging.HeaderPart headerPart =
mainPart.AddNewPart<
HeaderPart
>();
string rId = mainPart.GetIdOfPart(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());
}
}
IEnumerable<
DocumentFormat.OpenXml.Wordprocessing.SectionProperties
> sectPrs =
mainPart.Document.Body.Elements<
SectionProperties
>();
foreach (var sectPr in sectPrs)
{
sectPr.RemoveAllChildren<
HeaderReference
>();
sectPr.PrependChild<
HeaderReference
>(new HeaderReference() { Id = rId });
}
}
}