public static Wp.Drawing GetAnchorPicture(String imagePartId, uint width = 1500, uint height = 1500, uint horizontalOffset = 0, uint verticalOffset = 0, String pictureName = "Picture") { Wp.Drawing _drawing = new Wp.Drawing(); DWp.Anchor _anchor = new DWp.Anchor() { DistanceFromTop = (OXML.UInt32Value) 0U, DistanceFromBottom = (OXML.UInt32Value) 0U, DistanceFromLeft = (OXML.UInt32Value) 0U, DistanceFromRight = (OXML.UInt32Value) 0U, SimplePos = false, RelativeHeight = (OXML.UInt32Value) 0U, BehindDoc = true, Locked = false, LayoutInCell = true, AllowOverlap = true, EditId = "44CEF5E4", AnchorId = "44803ED1" }; DWp.SimplePosition _spos = new DWp.SimplePosition() { X = 0L, Y = 0L }; DWp.HorizontalPosition _hp = new DWp.HorizontalPosition() { RelativeFrom = DWp.HorizontalRelativePositionValues.Column }; DWp.PositionOffset _hPO = new DWp.PositionOffset(); _hPO.Text = horizontalOffset.ToString(); _hp.Append(_hPO); DWp.VerticalPosition _vp = new DWp.VerticalPosition() { RelativeFrom = DWp.VerticalRelativePositionValues.Paragraph }; DWp.PositionOffset _vPO = new DWp.PositionOffset(); _vPO.Text = verticalOffset.ToString(); _vp.Append(_vPO); DWp.Extent _e = new DWp.Extent() { Cx = height, Cy = width }; DWp.EffectExtent _ee = new DWp.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L }; DWp.WrapTight _wp = new DWp.WrapTight() { WrapText = DWp.WrapTextValues.BothSides }; DWp.WrapPolygon _wpp = new DWp.WrapPolygon() { Edited = false }; DWp.StartPoint _sp = new DWp.StartPoint() { X = 0L, Y = 0L }; DWp.LineTo _l1 = new DWp.LineTo() { X = 0L, Y = 0L }; DWp.LineTo _l2 = new DWp.LineTo() { X = 0L, Y = 0L }; DWp.LineTo _l3 = new DWp.LineTo() { X = 0L, Y = 0L }; DWp.LineTo _l4 = new DWp.LineTo() { X = 0L, Y = 0L }; _wpp.Append(_sp); _wpp.Append(_l1); _wpp.Append(_l2); _wpp.Append(_l3); _wpp.Append(_l4); _wp.Append(_wpp); DWp.DocProperties _dp = new DWp.DocProperties() { Id = 1U, Name = pictureName }; OXML.Drawing.Graphic _g = new OXML.Drawing.Graphic(); OXML.Drawing.GraphicData _gd = new OXML.Drawing.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }; OXML.Drawing.Pictures.Picture _pic = new OXML.Drawing.Pictures.Picture(); OXML.Drawing.Pictures.NonVisualPictureProperties _nvpp = new OXML.Drawing.Pictures.NonVisualPictureProperties(); OXML.Drawing.Pictures.NonVisualDrawingProperties _nvdp = new OXML.Drawing.Pictures.NonVisualDrawingProperties() { Id = 0, Name = pictureName }; OXML.Drawing.Pictures.NonVisualPictureDrawingProperties _nvpdp = new OXML.Drawing.Pictures.NonVisualPictureDrawingProperties(); _nvpp.Append(_nvdp); _nvpp.Append(_nvpdp); OXML.Drawing.Pictures.BlipFill _bf = new OXML.Drawing.Pictures.BlipFill(); OXML.Drawing.Blip _b = new OXML.Drawing.Blip() { Embed = imagePartId, CompressionState = OXML.Drawing.BlipCompressionValues.Print }; _bf.Append(_b); OXML.Drawing.Stretch _str = new OXML.Drawing.Stretch(); OXML.Drawing.FillRectangle _fr = new OXML.Drawing.FillRectangle(); _str.Append(_fr); _bf.Append(_str); OXML.Drawing.Pictures.ShapeProperties _shp = new OXML.Drawing.Pictures.ShapeProperties(); OXML.Drawing.Transform2D _t2d = new OXML.Drawing.Transform2D(); OXML.Drawing.Offset _os = new OXML.Drawing.Offset() { X = 0L, Y = 0L }; OXML.Drawing.Extents _ex = new OXML.Drawing.Extents() { Cx = 989965L, Cy = 791845L }; _t2d.Append(_os); _t2d.Append(_ex); OXML.Drawing.PresetGeometry _preGeo = new OXML.Drawing.PresetGeometry() { Preset = OXML.Drawing.ShapeTypeValues.Rectangle }; OXML.Drawing.AdjustValueList _adl = new OXML.Drawing.AdjustValueList(); _preGeo.Append(_adl); _shp.Append(_t2d); _shp.Append(_preGeo); _pic.Append(_nvpp); _pic.Append(_bf); _pic.Append(_shp); _gd.Append(_pic); _g.Append(_gd); _anchor.Append(_spos); _anchor.Append(_hp); _anchor.Append(_vp); _anchor.Append(_e); _anchor.Append(_ee); _anchor.Append(_wp); _anchor.Append(_dp); _anchor.Append(_g); _drawing.Append(_anchor); return(_drawing); }
private static Picture AddPicture(Slide slide, Shape referingShape, string imageFile) { Picture picture = new Picture(); string embedId = string.Empty; DocumentFormat.OpenXml.UInt32Value picId = 10001U; string name = string.Empty; if (slide.Elements <Picture>().Count() > 0) { picId = ++slide.Elements <Picture>().ToList().Last().NonVisualPictureProperties.NonVisualDrawingProperties.Id; } name = "image" + picId.ToString(); embedId = "rId" + (slide.Elements <Picture>().Count() + 915).ToString(); // some value NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties() { NonVisualDrawingProperties = new NonVisualDrawingProperties() { Name = name, Id = picId, Title = name }, NonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties() { PictureLocks = new DocumentFormat.OpenXml.Drawing.PictureLocks() { NoChangeAspect = true } }, ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties() { UserDrawn = true } }; BlipFill blipFill = new BlipFill() { Blip = new DocumentFormat.OpenXml.Drawing.Blip() { Embed = embedId } }; DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch() { FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle() }; blipFill.Append(stretch); ShapeProperties shapeProperties = new ShapeProperties() { Transform2D = new DocumentFormat.OpenXml.Drawing.Transform2D() { //MMS:4 Offset = new DocumentFormat.OpenXml.Drawing.Offset() { X = 1565275, Y = 612775 }, // { X = 457200L, Y = 1124000L } Extents = new DocumentFormat.OpenXml.Drawing.Extents() { Cx = 5486400, Cy = 4114800 } //{ Cx = 8229600L, Cy = 5029200L } } //<a:off x="1565275" y="612775" /> //<a:ext cx="5486400" cy="4114800" /> }; DocumentFormat.OpenXml.Drawing.PresetGeometry presetGeometry = new DocumentFormat.OpenXml.Drawing.PresetGeometry() { Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle }; DocumentFormat.OpenXml.Drawing.AdjustValueList adjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList(); presetGeometry.Append(adjustValueList); shapeProperties.Append(presetGeometry); picture.Append(nonVisualPictureProperties); picture.Append(blipFill); picture.Append(shapeProperties); slide.CommonSlideData.ShapeTree.Append(picture); // Add Image part AddImagePart(slide, embedId, imageFile); slide.Save(); return(picture); }