none
Inserted image not visible RRS feed

  • Question

  • I am trying to insert an image on specific spot in my word document using Open xml SDK.

    I can see the icon that the image can not be diaplay, so the image is there but can not be displayed.

        using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(link, true))
    	{
    		Text textPlaceHolder = wordDoc1.MainDocumentPart.Document.Body.Descendants<Text>().Where((x) => x.Text == "(Imageplaceholder)").FirstOrDefault();
    		if (textPlaceHolder == null)
    		{
    			Console.WriteLine("Text holder not found!");
    		}
    		else
    		{
    			var parent = textPlaceHolder.Parent;
    
    			if (!(parent is Run))  // Parent should be a run element.
    			{
    				Console.Out.WriteLine("Parent is not run");
    			}
    			else
    			{
    				var element =
    			new DocumentFormat.OpenXml.Wordprocessing.Drawing(
    				new DW.Inline(
    					new DW.Extent() { Cx = 480000L, Cy = 792000L },
    					new DW.EffectExtent()
    					{
    						LeftEdge = 980000L,
    						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 = "Test.jpg"
    									},
    									new PIC.NonVisualPictureDrawingProperties()),
    								new PIC.BlipFill(new A.Blip(
    										new A.BlipExtensionList(
    											new A.BlipExtension()
    											{
    												Uri =
    												"{28A0092B-C50C-407E-A947-70E740481C1C}"
    											})
    									)
    									{
    										Embed = "C:\\Users\\Me\\Desktop\\Test.jpg",
    										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"
    				});
    				
    				// Insert image (the image created with your function) after text place holder.        
    				textPlaceHolder.Parent.InsertAfter<DocumentFormat.OpenXml.Wordprocessing.Drawing>(element, textPlaceHolder);
    				// Remove text place holder.
    				textPlaceHolder.Remove();
    				wordDoc1.Close();
    			}
    		}  
    	}

    Thursday, October 5, 2017 3:34 PM

All replies

  • Hi Lio1972,

    I would suggest you add the image part first and then use its id to embed the picture.

    Here is the example.                 

      ImagePart imagePart = wordDoc1.MainDocumentPart.AddImagePart(ImagePartType.Jpeg);
    
                        string fileName = @"C:\Users\Me\Desktop\Test.jpg";
    
                        using (FileStream stream = new FileStream(fileName, FileMode.Open))
    
                        {
    
                            imagePart.FeedData(stream);
    
                        }
    
                        string ridID = wordDoc1.MainDocumentPart.GetIdOfPart(imagePart);

    Then in the element:

    Embed = "C:\\Users\\Me\\Desktop\\Test.jpg",
    
    Change To:
    
    Embed = ridID,

    You could refer to below link for more information.

    How to: Insert a picture into a word processing document (Open XML SDK)

    Best Regards,

    Terry


    MSDN Community Support Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 6, 2017 6:39 AM