Hello guys,
I can manipulate an existing word document header in a SharePoint library and insert a text header to it.
However, I am having some chanllenges adding an image to a header into a word document here is my code. When I run this code I don't get to see the image in the header, all I see is x image which says image cannot be displayed.
Please assist
static void Main(string[] args) { string siteUrl = "http://moss"; using (SPSite spSite = new SPSite(siteUrl)) { Console.WriteLine("Querying for doc.docx"); SPList list = spSite.RootWeb.Lists["Test"]; SPQuery query = new SPQuery(); query.ViewFields = @"<FieldRef Name='FileLeafRef' />"; query.Query = @"<Where> <Eq> <FieldRef Name='FileLeafRef' /> <Value Type='Text'>doc.docx</Value> </Eq> </Where>"; SPListItemCollection collection = list.GetItems(query); if (collection.Count != 1) { Console.WriteLine("doc.docx not found"); Environment.Exit(0); } Console.WriteLine("Opening"); SPFile file = collection[0].File; byte[] byteArray = file.OpenBinary(); const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; const string relationshipNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; string reference = "w:headerReference"; using (MemoryStream memStr = new MemoryStream()) { memStr.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(memStr, true)) { Document document = wordDoc.MainDocumentPart.Document; document.MainDocumentPart.DeleteParts(document.MainDocumentPart.FooterParts); document.MainDocumentPart.DeleteParts(document.MainDocumentPart.HeaderParts); AddPicHeader(wordDoc); } Console.WriteLine("Saving"); string linkFileName = file.Item["LinkFilename"] as string; file.ParentFolder.Files.Add(linkFileName, memStr, true); } } } public static Header Mkpicheader(WordprocessingDocument doc, string relationshipId) { //Define the reference of the image. var element = new Drawing( new DW.Inline( new DW.Extent() { Cx = 990000L, Cy = 792000L }, new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L }, new DW.DocProperties() { Id = (UInt32Value)1U, Name = "Picture 1" }, new DW.NonVisualGraphicFrameDrawingProperties( new A.GraphicFrameLocks() { NoChangeAspect = true }), new A.Graphic( new A.GraphicData( new PIC.Picture( new PIC.NonVisualPictureProperties( new PIC.NonVisualDrawingProperties() { Id = (UInt32Value)0U, Name = "oando-logo.png" }, new PIC.NonVisualPictureDrawingProperties()), new PIC.BlipFill( new A.Blip( new A.BlipExtensionList( new A.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }) ) { Embed = relationshipId, CompressionState = A.BlipCompressionValues.Print }, new A.Stretch( new A.FillRectangle())), new PIC.ShapeProperties( new A.Transform2D( new A.Offset() { X = 0L, Y = 0L }, new A.Extents() { Cx = 990000L, Cy = 792000L }), new A.PresetGeometry( new A.AdjustValueList() ) { Preset = A.ShapeTypeValues.Rectangle })) ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }) ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U, EditId = "50D07946" }); var header = new Header(); var paragraph = new Paragraph(); var run = new Run(); run.Append(element); paragraph.Append(run); header.Append(paragraph); return header; //newHeaderPart.Header = header; // wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element))); } public static void AddPicHeader(WordprocessingDocument doc) { ImagePart imgPart=doc.MainDocumentPart.AddImagePart(ImagePartType.Png); var imagePartID = doc.MainDocumentPart.GetIdOfPart(imgPart); using (Stream targetStream = imgPart.GetStream()) { using (FileStream sourceStream = new FileStream("../../officelogo.gif", FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024]; int nrBytesWritten = sourceStream.Read(buffer, 0, 1024); while (nrBytesWritten > 0) { targetStream.Write(buffer, 0, nrBytesWritten); nrBytesWritten = sourceStream.Read(buffer, 0, 1024); } } } if (doc.MainDocumentPart.HeaderParts.Count() == 0) { doc.MainDocumentPart.DeleteParts(doc.MainDocumentPart.HeaderParts); var newHeaderPart = doc.MainDocumentPart.AddNewPart<HeaderPart>(); var rId = doc.MainDocumentPart.GetIdOfPart(newHeaderPart); var headerRef = new HeaderReference(); headerRef.Id = rId; var sectionProps = doc.MainDocumentPart.Document.Body.Elements<SectionProperties>().LastOrDefault(); if (sectionProps == null) { sectionProps = new SectionProperties(); doc.MainDocumentPart.Document.Body.Append(sectionProps); } sectionProps.RemoveAllChildren<HeaderReference>(); sectionProps.Append(headerRef); newHeaderPart.Header=Mkpicheader(doc,imagePartID); newHeaderPart.Header.Save(); } }
I think your issue is that your adding the image to the MainDocumentPart rather than the part the image resides in (header). You'll may need to use AddNewPart<ImagePart>("image/png", null) rather than AddImagePart.