public void InsertImageInLastSlide(Slide slide, string imagePath, string imageExt) { // Creates an Picture instance and adds its children. P.Picture picture = new P.Picture(); string embedId = string.Empty; embedId = "rId" + (slide.Elements().Count() + 915).ToString(); P.NonVisualPictureProperties nonVisualPictureProperties = new P.NonVisualPictureProperties( new P.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Picture 5" }, new P.NonVisualPictureDrawingProperties(new A.PictureLocks() { NoChangeAspect = true }), new ApplicationNonVisualDrawingProperties()); P.BlipFill blipFill = new P.BlipFill(); Drawing.Blip blip = new Drawing.Blip() { Embed = embedId }; // Creates an BlipExtensionList instance and adds its children Drawing.BlipExtensionList blipExtensionList = new Drawing.BlipExtensionList(); Drawing.BlipExtension blipExtension = new Drawing.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi useLocalDpi = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi() { Val = false }; useLocalDpi.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); blipExtension.Append(useLocalDpi); blipExtensionList.Append(blipExtension); blip.Append(blipExtensionList); Drawing.Stretch stretch = new Drawing.Stretch(); Drawing.FillRectangle fillRectangle = new Drawing.FillRectangle(); stretch.Append(fillRectangle); blipFill.Append(blip); blipFill.Append(stretch); // Creates an ShapeProperties instance and adds its children. P.ShapeProperties shapeProperties = new P.ShapeProperties(); A.Transform2D transform2D = new A.Transform2D(); A.Offset offset = new A.Offset() { X = 2096400L, Y = 441000L }; A.Extents extents = new A.Extents() { Cx = 8000000L, Cy = 6000000L }; transform2D.Append(offset); transform2D.Append(extents); A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle }; A.AdjustValueList adjustValueList = new A.AdjustValueList(); presetGeometry.Append(adjustValueList); shapeProperties.Append(transform2D); shapeProperties.Append(presetGeometry); picture.Append(nonVisualPictureProperties); picture.Append(blipFill); picture.Append(shapeProperties); slide.CommonSlideData.ShapeTree.AppendChild(picture); // Generates content of imagePart. ImagePart imagePart = slide.SlidePart.AddNewPart <ImagePart>(imageExt, embedId); FileStream fileStream = new FileStream(imagePath, FileMode.Open); imagePart.FeedData(fileStream); fileStream.Close(); }
public static void AddImage(string file, string image) { using (var presentation = PresentationDocument.Open(file, true)) { var slidePart = presentation .PresentationPart .SlideParts .ElementAt(u); //.First(); var part = slidePart .AddImagePart(ImagePartType.Png); using (var stream = File.OpenRead(image)) { part.FeedData(stream); } var tree = slidePart .Slide .Descendants <DocumentFormat.OpenXml.Presentation.ShapeTree>() .First(); var picture = new DocumentFormat.OpenXml.Presentation.Picture(); picture.NonVisualPictureProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties(); picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties { Name = "My Shape", Id = (UInt32)tree.ChildElements.Count - 1 }); var nonVisualPictureDrawingProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties(); nonVisualPictureDrawingProperties.Append(new DocumentFormat.OpenXml.Drawing.PictureLocks() { NoChangeAspect = true }); picture.NonVisualPictureProperties.Append(nonVisualPictureDrawingProperties); picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties()); var blipFill = new DocumentFormat.OpenXml.Presentation.BlipFill(); var blip1 = new DocumentFormat.OpenXml.Drawing.Blip() { Embed = slidePart.GetIdOfPart(part) }; var blipExtensionList1 = new DocumentFormat.OpenXml.Drawing.BlipExtensionList(); var blipExtension1 = new DocumentFormat.OpenXml.Drawing.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; var useLocalDpi1 = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi() { Val = false }; useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); blipExtension1.Append(useLocalDpi1); blipExtensionList1.Append(blipExtension1); blip1.Append(blipExtensionList1); var stretch = new DocumentFormat.OpenXml.Drawing.Stretch(); stretch.Append(new DocumentFormat.OpenXml.Drawing.FillRectangle()); blipFill.Append(blip1); blipFill.Append(stretch); picture.Append(blipFill); picture.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); picture.ShapeProperties.Transform2D = new DocumentFormat.OpenXml.Drawing.Transform2D(); picture.ShapeProperties.Transform2D.Append(new DocumentFormat.OpenXml.Drawing.Offset { X = 0, Y = 0, }); picture.ShapeProperties.Transform2D.Append(new DocumentFormat.OpenXml.Drawing.Extents { Cx = 1000000, Cy = 1000000, }); picture.ShapeProperties.Append(new DocumentFormat.OpenXml.Drawing.PresetGeometry { Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle }); tree.Append(picture); } }
/// <summary> /// Builds out image as a Picture shape using BlipFill and appends it to the slide shapetree, using a shape from SlideLayout to determine size and location of image. /// </summary> /// <param name="slide">The slide to insert the image</param> /// <param name="layoutPlaceholderShape">The shape where the image will be inserted</param> /// <param name="imageLocation">URL to the local image file</param> /// <param name="shapeNumber">A unique positive integer for the shape relationship ID</param> public static void AddImageToSlide(this SlidePart slide, Shape layoutPlaceholderShape, string imageLocation, uint shapeNumber) { ShapeProperties shapeProperties = new ShapeProperties(); // Generate a unique relationship ID to avoid collision string embedId = "rId" + (slide.Slide.Elements().Count() + 100 + shapeNumber).ToString(); // Determine the image location from layout placeholder shape Drawing.Offset imageOffset = (Drawing.Offset)(layoutPlaceholderShape).ShapeProperties.Transform2D.Offset.Clone(); Drawing.Extents imageExtents = (Drawing.Extents)(layoutPlaceholderShape).ShapeProperties.Transform2D.Extents.Clone(); Drawing.Transform2D imageTransform = new Drawing.Transform2D(); imageTransform.Append(imageExtents); imageTransform.Offset = imageOffset; shapeProperties.Append(imageTransform); // Preset geometry Drawing.PresetGeometry presetGeometry = new Drawing.PresetGeometry() { Preset = Drawing.ShapeTypeValues.Rectangle }; Drawing.AdjustValueList adjustValueList = new Drawing.AdjustValueList(); presetGeometry.Append(adjustValueList); shapeProperties.Append(presetGeometry); // Generate blip extension Drawing.BlipExtensionList blipExtensionList = new Drawing.BlipExtensionList(); Drawing.BlipExtension blipExtension = new Drawing.BlipExtension() { Uri = $"{{{Guid.NewGuid().ToString()}}}" }; // Local DPI (dots per square inch) DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi useLocalDpi = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi() { Val = false }; useLocalDpi.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); blipExtension.Append(useLocalDpi); blipExtensionList.Append(blipExtension); // Set up blipfill for image Drawing.Blip imageBlip = new Drawing.Blip(); Drawing.Stretch stretch = new Drawing.Stretch(new Drawing.FillRectangle()); BlipFill blipFill = new BlipFill(); imageBlip.Append(blipExtensionList); blipFill.Append(imageBlip); blipFill.Append(stretch); // Set up picture and nonvisual properties Picture picture = new Picture(); NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties( new NonVisualDrawingProperties() { Id = (50U + shapeNumber), Name = "Picture " + shapeNumber }, new NonVisualPictureDrawingProperties(new Drawing.PictureLocks() { NoChangeAspect = true }), new ApplicationNonVisualDrawingProperties() ); picture.Append(nonVisualPictureProperties); picture.Append(blipFill); picture.Append(shapeProperties); //s.Parent.ReplaceChild(P, s); slide.Slide.CommonSlideData.ShapeTree.AppendChild(picture); // Determine ImagePart type ImagePart imagePart; switch (Path.GetExtension(imageLocation)) { case ".jpg": case ".jpeg": imagePart = slide.AddImagePart(ImagePartType.Jpeg, embedId); break; case ".png": imagePart = slide.AddImagePart(ImagePartType.Png, embedId); break; case ".gif": imagePart = slide.AddImagePart(ImagePartType.Gif, embedId); break; case ".ico": imagePart = slide.AddImagePart(ImagePartType.Icon, embedId); break; default: throw new ArgumentException($"Error: Image file type '{Path.GetExtension(imageLocation)}' for file '{Path.GetFileNameWithoutExtension(imageLocation)}' not recognized!"); } // Read the image file as a stream to ImagePart using (FileStream fs = File.OpenRead(imageLocation)) { fs.Seek(0, SeekOrigin.Begin); imagePart.FeedData(fs); } imageBlip.Embed = embedId; }