/// <summary> /// Adds an image part to the header and returns the part ID /// </summary> /// <param name="stream"></param> /// <param name="contentType"></param> /// <returns></returns> protected override string AddImagePart(Stream stream, string contentType) { var imagePart = headerPart.AddImagePart(contentType); imagePart.FeedData(stream); return(headerPart.GetIdOfPart(imagePart)); }
public static void AddHeader(MainDocumentPart mainDocPart, ReportImage reportImge) { // Delete the existing header parts. mainDocPart.DeleteParts(mainDocPart.HeaderParts); // Create a new header part and get its relationship id. HeaderPart newHeaderPart = mainDocPart.AddNewPart <HeaderPart>(); string rId = mainDocPart.GetIdOfPart(newHeaderPart); ImagePart imagepart = newHeaderPart.AddImagePart(ImagePartType.Jpeg); FileInfo newImg = new FileInfo(reportImge.Value); using (FileStream stream = newImg.OpenRead()) { imagepart.FeedData(stream); } string imageRID = newHeaderPart.GetIdOfPart(imagepart); reportImge.RId = imageRID; Header header = GeneratePageHeaderPart(reportImge); header.Save(newHeaderPart); foreach (SectionProperties sectProperties in mainDocPart.Document.Descendants <SectionProperties>()) { // Delete any existing references to headers. foreach (HeaderReference headerReference in sectProperties.Descendants <HeaderReference>()) { sectProperties.RemoveChild(headerReference); } HeaderReference newHeaderReference = new HeaderReference() { Id = rId, Type = HeaderFooterValues.Default }; sectProperties.Append(newHeaderReference); } header.Save(); }