How to set relationship Id's properly when adding an ImagePart RRS feed

  • Question

  • Hi,

    I am working with OOXML SDK 2.5 and a Word .docx document.

    My objective is to create a method that will create a Paragraph containing an image from a byte array.

    When I add the paragraph to a the document body, I want to see the image in the document.

    The code below works for adding one image to a document, but as soon as I add multiple images,

    all the image links appear broken in the Word document.

    When I look at the generated document with the Productivity Tool, the relationship id is always 'rId4', 

    regardless of the parameters to the AddImagePart method.

    Can anybody assist please?  (Have burnt over 10 hours trying to debug this, and was just about to hit

    Microsoft paid support, but the site is down at the moment.)

    public static Paragraph CreateImageParagraph(WordprocessingDocument wordprocessingDocument, byte[] pngBytes) { var dimesionsInEmus = GetImageEmus(new MemoryStream(pngBytes), GetPageWidthInEmus(wordprocessingDocument)); MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart; ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png); imagePart.FeedData(new MemoryStream(pngBytes)); var relationshipId = mainPart.GetIdOfPart(imagePart); // for image properties: var guid = Guid.NewGuid().ToString(); string uniqueImageUri = String.Format("{{{0}}}", guid); var element = new Drawing( new DW.Inline( new DW.Extent() { Cx = dimesionsInEmus.Width, Cy = dimesionsInEmus.Height }, new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L }, new DW.DocProperties() { Id = (UInt32Value)1U, Name = guid }, 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 = guid + ".png" }, new PIC.NonVisualPictureDrawingProperties()), new PIC.BlipFill( new A.Blip( new A.BlipExtensionList( new A.BlipExtension() { Uri = uniqueImageUri }) ) { 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 = dimesionsInEmus.Width, Cy = dimesionsInEmus.Height }), new A.PresetGeometry( new A.AdjustValueList() ) { Preset = A.ShapeTypeValues.Rectangle })) ) { Uri = "" }) ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U }); return new Paragraph(new Run(element)); }

    Monday, November 25, 2013 5:46 AM

All replies

  • You just provide a code snippet, I edit it as below and find it cannot add the picture correctly.

    Did I miss anything?

    Image img = Image.FromFile(@"C:\Pictures\image1.png");
    byte[] byimg = imageToByteArray(img);
    string fileName = @"C:\Documents\DocPicture.docx";
    using (WordprocessingDocument myDocument = WordprocessingDocument.Open(fileName, true))
        Paragraph paragraph1 = CreateImageParagraph(myDocument, byimg);
        Body body1 = myDocument.MainDocumentPart.Document.Body;

    Tuesday, November 26, 2013 7:05 AM