none
OpenXml C# generated word document wont show embedded image RRS feed

  • Question

  • Hello,

    I've been trying to create an auxiliar class to help me export a word document. So far I've been able to do whatever I want with the document as long as there is no image in it.

    Everytime I try to insert an image, the generated document won't open properly on Word. I't gives me an error message "error not specified" on opening the document and won't show the image.

    The more strange, the behavior varies at random, and sometimes the generated document will come up correct and show the images.

    I'm using Anchor and Inline to get text aligned or floating images. I generate the element depending on a parameter passed (inlined or floating) to the method and append the resulting element into a paragraph. I embed the image creating an ImagePart and using FeedData to fill it up and passing it's relationship ID so I can use it in the code generating the element (and reuse it for optimization wherever possible).

    I've used the code generated by the Productivity Tool from Open XML SDK 2.0 to create my generic image embedding method. I've tried to validate the document generated on the Tool but i't says there are no schema validation errors on the package or any of the parts/element inside.

    Follow an example of a generated document and the code I use to create the element (Inline or Anchor):

    Generated Document

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Threading;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using A = DocumentFormat.OpenXml.Drawing;
    using Pic = DocumentFormat.OpenXml.Drawing.Pictures;
    using Wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    
    public Run CreateImageRun(String AImgPartId, Int64 AWidth, Int64 AHeight, Boolean AInline = false, Int64 ATop = 0, Int64 ALeft = 0)
    		{
    			Drawing LDrawing = new Drawing();
    
    			OpenXmlCompositeElement LImgContainer;
    
    			if (AInline)
    			{
    				LImgContainer = new Wp.Inline()
    				{
    					DistanceFromTop = (UInt32Value)0U,
    					DistanceFromBottom = (UInt32Value)0U,
    					DistanceFromLeft = (UInt32Value)0U,
    					DistanceFromRight = (UInt32Value)0U
    				};
    			}
    			else
    			{
    				LImgContainer = new Wp.Anchor()
    				{
    					DistanceFromTop = (UInt32Value)0U,
    					DistanceFromBottom = (UInt32Value)0U,
    					DistanceFromLeft = (UInt32Value)0U,
    					DistanceFromRight = (UInt32Value)0U,
    					RelativeHeight = (UInt32Value)251658240U,
    					SimplePos = false,
    					BehindDoc = false,
    					Locked = false,
    					LayoutInCell = true,
    					AllowOverlap = true,
    					SimplePosition = new Wp.SimplePosition { X = 0L, Y = 0L },
    					HorizontalPosition = new Wp.HorizontalPosition()
    					{
    						RelativeFrom = Wp.HorizontalRelativePositionValues.Page,
    						PositionOffset = new Wp.PositionOffset { Text = (ALeft * 3600).ToString() }
    					},
    					VerticalPosition = new Wp.VerticalPosition()
    					{
    						RelativeFrom = Wp.VerticalRelativePositionValues.Page,
    						PositionOffset = new Wp.PositionOffset { Text = (ATop * 3600).ToString() }
    					}
    				};
    			}
    
    			CreateImgGraphic(AImgPartId, LImgContainer, AWidth, AHeight);
    
    			if (!AInline)
    			{
    				LImgContainer.InsertAfter(new Wp.WrapNone(), LImgContainer.Elements<Wp.EffectExtent>().First());
    			}
    			LDrawing.Append(LImgContainer);
    
    			return new Run(LDrawing);
    		}
    
    		private void CreateImgGraphic(String AImgPartId, OpenXmlCompositeElement AImgContainer, Int64 AWidth, Int64 AHeight)
    		{
    			Wp.Extent extent1 = new Wp.Extent()
    			{
    				Cx = AWidth * 3600,
    				Cy = AHeight * 3600
    			};
    			Wp.EffectExtent effectExtent1 = new Wp.EffectExtent()
    			{
    				LeftEdge = 0L,
    				TopEdge = 0L,
    				RightEdge = 0L,
    				BottomEdge = 0L
    			};
    			Wp.DocProperties docProperties1 = new Wp.DocProperties()
    			{
    				Id = (UInt32Value)DateTime.Now.Ticks,
    				Name = DateTime.Now.Ticks.ToString("X")
    			};
    
    			Wp.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();
    
    			A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks() { NoChangeAspect = true };
    			graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
    
    			nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);
    
    			A.Graphic graphic1 = new A.Graphic();
    			graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
    
    			A.GraphicData graphicData1 = new A.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" };
    
    			Pic.Picture picture1 = new Pic.Picture();
    			picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");
    
    			Pic.NonVisualPictureProperties nonVisualPictureProperties1 = new Pic.NonVisualPictureProperties();
    			Pic.NonVisualDrawingProperties nonVisualDrawingProperties1 = new Pic.NonVisualDrawingProperties()
    			{
    				Id = (UInt32Value)DateTime.Now.Ticks,
    				Name = DateTime.Now.Ticks.ToString("X")
    			};
    
    			Pic.NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new Pic.NonVisualPictureDrawingProperties();
    			A.PictureLocks pictureLocks1 = new A.PictureLocks()
    			{
    				NoChangeAspect = true,
    				NoChangeArrowheads = true
    			};
    
    			nonVisualPictureDrawingProperties1.Append(pictureLocks1);
    
    			nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
    			nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);
    
    			Pic.BlipFill blipFill1 = new Pic.BlipFill();
    			A.Blip blip1 = new A.Blip()
    			{
    				Embed = AImgPartId,
    				CompressionState = A.BlipCompressionValues.Print
    			};
    			A.SourceRectangle sourceRectangle1 = new A.SourceRectangle();
    
    			A.Stretch stretch1 = new A.Stretch();
    			A.FillRectangle fillRectangle1 = new A.FillRectangle();
    
    			stretch1.Append(fillRectangle1);
    
    			blipFill1.Append(blip1);
    			blipFill1.Append(sourceRectangle1);
    			blipFill1.Append(stretch1);
    
    			Pic.ShapeProperties shapeProperties1 = new Pic.ShapeProperties()
    			{
    				BlackWhiteMode = A.BlackWhiteModeValues.Auto
    			};
    
    			A.Transform2D transform2D1 = new A.Transform2D();
    			A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L };
    			A.Extents extents1 = new A.Extents()
    			{
    				Cx = AWidth * 3600,
    				Cy = AHeight * 3600
    			};
    
    			transform2D1.Append(offset1);
    			transform2D1.Append(extents1);
    
    			A.PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
    			A.AdjustValueList adjustValueList1 = new A.AdjustValueList();
    
    			presetGeometry1.Append(adjustValueList1);
    			A.NoFill noFill1 = new A.NoFill();
    
    			A.Outline outline1 = new A.Outline() { Width = 9525 };
    			A.NoFill noFill2 = new A.NoFill();
    			A.Miter miter1 = new A.Miter() { Limit = 800000 };
    			A.HeadEnd headEnd1 = new A.HeadEnd();
    			A.TailEnd tailEnd1 = new A.TailEnd();
    
    			outline1.Append(noFill2);
    			outline1.Append(miter1);
    			outline1.Append(headEnd1);
    			outline1.Append(tailEnd1);
    
    			shapeProperties1.Append(transform2D1);
    			shapeProperties1.Append(presetGeometry1);
    			shapeProperties1.Append(noFill1);
    			shapeProperties1.Append(outline1);
    
    			picture1.Append(nonVisualPictureProperties1);
    			picture1.Append(blipFill1);
    			picture1.Append(shapeProperties1);
    
    			graphicData1.Append(picture1);
    
    			graphic1.Append(graphicData1);
    
    			AImgContainer.Append(extent1);
    			AImgContainer.Append(effectExtent1);
    			AImgContainer.Append(docProperties1);
    			AImgContainer.Append(nonVisualGraphicFrameDrawingProperties1);
    			AImgContainer.Append(graphic1);
    		}
    Sorry for the messy code havent had the time to really clena it up...

    Friday, February 22, 2013 12:55 PM

All replies

  • Hi Getulio,

    Thank you for posting in the MSDN Forum.

    Is this all your code? It will be nice if you could create a sample project which can reproduce your scenario and upload it to SkyDrive. 

    Meanwhile, you can refer to this page for the sample which adds images to a document.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 25, 2013 11:22 AM
    Moderator
  • Hi Quist Zhang,

    Thank you for your answer.

    This is not all of the code, just the part that generates the "Run" containing the graphics filled by the image. I've abstracted it from code generated by the Productivity Tool to take an image id of an already embedded image and create a "Run" that can be placed in a paragraph anywhere on the document.

    I'l see if I can provide a more solid part of the code on a sample project that can actually generate a document for testing.

    Thank you for your time

    Monday, February 25, 2013 12:01 PM
  • Hi Getulio,

    Thank you for your last reply and I look forward to your update.

    Regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 26, 2013 5:37 AM
    Moderator