Document doc = new Document();
doc.LoadFromFile(@"Template.docx");
Image newImage = Image.FromFile(@"E-iceblue.png");
DocPicture newPic = new DocPicture(doc);
newPic.LoadImage(newImage);
Section section = doc.Sections[0];
HeaderFooter header = section.HeadersFooters.Header;
foreach (Paragraph para in header.Paragraphs)
{
List<DocumentObject> pictures = new List<DocumentObject>();
//Get all pictures in the header
foreach (DocumentObject docObj in para.ChildObjects)
{
if (docObj.DocumentObjectType == DocumentObjectType.Picture)
{
pictures.Add(docObj);
}
}
//Replace pitures with the 'newPic'"
foreach (DocumentObject pic in pictures)
{
int index = para.ChildObjects.IndexOf(pic);
para.ChildObjects.Insert(index, newPic);
para.ChildObjects.Remove(pic);
}
}
doc.SaveToFile("ReplaceWithImage.docx", FileFormat.Docx);