/// <summary> /// Insert Image into Slide /// </summary> /// <param name="filePath">PowerPoint Path</param> /// <param name="imagePath">Image Path</param> /// <param name="imageExt">Image Extension</param> public void InsertImageInLastSlide(Slide slide, string imagePath, string imageExt) { // Creates a Picture instance and adds its children. P.Picture picture = new P.Picture(); string embedId = string.Empty; embedId = "rId" + (slide.Elements <P.Picture>().Count() + 915).ToString(); P.NonVisualPictureProperties nonVisualPictureProperties = new P.NonVisualPictureProperties( new P.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Picture 5" }, new P.NonVisualPictureDrawingProperties(new D.PictureLocks() { NoChangeAspect = true }), new ApplicationNonVisualDrawingProperties()); P.BlipFill blipFill = new P.BlipFill(); Blip blip = new Blip() { Embed = embedId }; // Creates a BlipExtensionList instance and adds its children BlipExtensionList blipExtensionList = new BlipExtensionList(); BlipExtension blipExtension = new BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; UseLocalDpi useLocalDpi = new UseLocalDpi() { Val = false }; useLocalDpi.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); blipExtension.Append(useLocalDpi); blipExtensionList.Append(blipExtension); blip.Append(blipExtensionList); Stretch stretch = new Stretch(); FillRectangle fillRectangle = new FillRectangle(); stretch.Append(fillRectangle); blipFill.Append(blip); blipFill.Append(stretch); // Creates a ShapeProperties instance and adds its children. P.ShapeProperties shapeProperties = new P.ShapeProperties(); D.Transform2D transform2D = new D.Transform2D(); D.Offset offset = new D.Offset() { X = 457200L, Y = 1524000L }; D.Extents extents = new D.Extents() { Cx = 8229600L, Cy = 5029200L }; transform2D.Append(offset); transform2D.Append(extents); D.PresetGeometry presetGeometry = new D.PresetGeometry() { Preset = D.ShapeTypeValues.Rectangle }; D.AdjustValueList adjustValueList = new D.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>(@"image/" + 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> /// 在指定位置添加图片 支持 png jpeg gif. /// </summary> public Picture AddPicture(SlidePart sldpart, string filePath, D.Transform2D transform) { if (!File.Exists(filePath)) { return(null); } var imgprt = sldpart.AddImagePart(AnalysisHelper.GetImagePartType(filePath)); string rlid = sldpart.GetIdOfPart(imgprt); var tree = sldpart.Slide.CommonSlideData.ShapeTree; uint maxid = AnalysisHelper.GetMaxId(tree); Picture pic = new Picture(); pic.NonVisualPictureProperties = new NonVisualPictureProperties ( new P.NonVisualDrawingProperties() { Id = maxid + 1, Name = $"PIC{maxid + 1}" }, new P.NonVisualPictureDrawingProperties() { PictureLocks = new D.PictureLocks() { NoChangeAspect = true } }, //必须指定元素的类型,否则会报错。 new ApplicationNonVisualDrawingProperties ( new PlaceholderShape() { Type = PlaceholderValues.Picture } ) ); P.BlipFill blipFill = new P.BlipFill(); D.Blip blip = new D.Blip() { Embed = rlid }; D.BlipExtensionList blipExtensionList = new D.BlipExtensionList(); D.BlipExtension blipExtension = new D.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); D.Stretch stretch = new D.Stretch(); D.FillRectangle fillRectangle = new D.FillRectangle(); stretch.Append(fillRectangle); blipFill.Append(blip); blipFill.Append(stretch); pic.Append(blipFill); pic.ShapeProperties = new P.ShapeProperties ( new D.PresetGeometry(new D.AdjustValueList()) { Preset = D.ShapeTypeValues.Rectangle } ) { Transform2D = transform }; tree.AppendChild(pic); using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) { imgprt.FeedData(fileStream); } return(pic); }
private static void AddImage(string file, List <ImagePath> image) { using (var presentationDocument = PresentationDocument.Open(file, true)) { var slideCount = presentationDocument.PresentationPart.SlideParts.Count(); SlideIdList slideIdList = presentationDocument.PresentationPart.Presentation.SlideIdList; Presentation presentation = presentationDocument.PresentationPart.Presentation; PresentationPart presentationPart = presentationDocument.PresentationPart; OpenXmlElementList slideIds = presentationPart.Presentation.SlideIdList.ChildElements; int cnt = 0; // 画像の添付数 int j = 0; // 画像添付スライド位置 string relId = (slideIds[j] as SlideId).RelationshipId; SlidePart slidePart = (SlidePart)presentationPart.GetPartById(relId); foreach (ImagePath imgPath in image) { ImagePart part = slidePart .AddImagePart(ImagePartType.Png); using (var stream = File.OpenRead(imgPath.Path)) { 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 Drawing2.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 Drawing2.Blip() { Embed = slidePart.GetIdOfPart(part) }; var blipExtensionList1 = new Drawing2.BlipExtensionList(); var blipExtension1 = new Drawing2.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 Drawing2.Stretch(); stretch.Append(new Drawing2.FillRectangle()); blipFill.Append(blip1); blipFill.Append(stretch); picture.Append(blipFill); picture.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); picture.ShapeProperties.Transform2D = new Drawing2.Transform2D(); int rotation = 0; switch (imgPath.Rot) { case RotateFlipType.RotateNoneFlipNone: rotation = 0; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION0[cnt % 6].X, Y = POSITION0[cnt % 6].Y, }); break; case RotateFlipType.Rotate180FlipNone: // 時計回りに180度回転しているので、180度回転して戻す rotation = 60000 * 180; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION0[cnt % 6].X, Y = POSITION0[cnt % 6].Y, }); break; case RotateFlipType.Rotate90FlipNone: // 時計回りに270度回転しているので、90度回転して戻す rotation = 60000 * 90; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION90[cnt % 6].X, Y = POSITION90[cnt % 6].Y, }); break; case RotateFlipType.Rotate270FlipNone: // 時計回りに90度回転しているので、270度回転して戻す rotation = 60000 * 270; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION90[cnt % 6].X, Y = POSITION90[cnt % 6].Y, }); break; default: rotation = 0; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION0[cnt % 6].X, Y = POSITION0[cnt % 6].Y, }); break; } picture.ShapeProperties.Transform2D.Rotation = rotation; // 縦向き picture.ShapeProperties.Transform2D.Append(new Drawing2.Extents { Cx = imgPath.Size.Width, Cy = imgPath.Size.Height, }); picture.ShapeProperties.Append(new Drawing2.PresetGeometry { Preset = Drawing2.ShapeTypeValues.Rectangle }); tree.Append(picture); if (cnt % 6 == 5) { if (j < slideCount - 1) { j++; relId = (slideIds[j] as SlideId).RelationshipId; slidePart = (SlidePart)presentationPart.GetPartById(relId); } else { // 画像ループを抜ける break; } } cnt++; } // スライドを削除する for (int i = slideCount - 1; i > j; i--) { //Console.WriteLine(i); SlideId slideId = slideIds[i] as SlideId; string slideRelId = slideId.RelationshipId; slideIdList.RemoveChild(slideId); if (presentation.CustomShowList != null) { // Iterate through the list of custom shows. foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>()) { if (customShow.SlideList != null) { // Declare a link list of slide list entries. LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>(); foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements()) { // Find the slide reference to remove from the custom show. if (slideListEntry.Id != null && slideListEntry.Id == slideRelId) { slideListEntries.AddLast(slideListEntry); } } // Remove all references to the slide from the custom show. foreach (SlideListEntry slideListEntry in slideListEntries) { customShow.SlideList.RemoveChild(slideListEntry); } } } } presentation.Save(); SlidePart slidePart2 = presentationPart.GetPartById(slideRelId) as SlidePart; presentationPart.DeletePart(slidePart2); } } }
private void insertImage(Slide slide, Size maxSize, string imagePath, int imgId) { P.Picture picture = new P.Picture(); string embedId = string.Empty; string imageExt = getImageType(imagePath); Size imgSize = new ImageInfoUtils().getPPTSize(imagePath); embedId = "rId" + imgId.ToString(); P.NonVisualPictureProperties nonVisualPictureProperties = new P.NonVisualPictureProperties( new P.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Picture " + imgId }, new P.NonVisualPictureDrawingProperties(new A.PictureLocks() { NoChangeAspect = true }), new P.ApplicationNonVisualDrawingProperties()); picture.Append(nonVisualPictureProperties); UseLocalDpi useLocalDpi = new UseLocalDpi() { Val = false }; useLocalDpi.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); BlipExtension blipExtension = new BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; blipExtension.Append(useLocalDpi); BlipExtensionList blipExtensionList = new BlipExtensionList(); blipExtensionList.Append(blipExtension); Stretch stretch = new Stretch(); FillRectangle fillRectangle = new FillRectangle(); stretch.Append(fillRectangle); P.ShapeProperties shapeProperties = new P.ShapeProperties() { Transform2D = new A.Transform2D() { Offset = new A.Offset() { X = (maxSize.Width - imgSize.Width) / 2, Y = (maxSize.Height - imgSize.Height) / 2 }, Extents = new A.Extents() { Cx = imgSize.Width, Cy = imgSize.Height } } }; shapeProperties.Append(new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle, AdjustValueList = new A.AdjustValueList() }); Blip blip = new Blip() { Embed = embedId }; blip.Append(blipExtensionList); P.BlipFill blipFill = new P.BlipFill() { Blip = blip }; blipFill.Append(stretch); picture.Append(blipFill); picture.Append(shapeProperties); slide.CommonSlideData.ShapeTree.AppendChild(picture); ImagePart imagePart = slide.SlidePart.AddNewPart <ImagePart>(imageExt, embedId); FileStream fileStream = new FileStream(imagePath, FileMode.Open); imagePart.FeedData(fileStream); fileStream.Close(); }
private void insert(SlidePart slidePart, string imagePath, string imgIdx, uint uid) { P.Picture picture = new P.Picture(); string embedId = imgIdx; P.NonVisualPictureProperties nonVisualPictureProperties = new P.NonVisualPictureProperties( new P.NonVisualDrawingProperties() { Id = uid--, Name = "Picture 5" }, new P.NonVisualPictureDrawingProperties(new A.PictureLocks() { NoChangeAspect = true }), new ApplicationNonVisualDrawingProperties()); P.BlipFill blipFill = new P.BlipFill(); BlipExtensionList blipExtensionList = new BlipExtensionList(); BlipExtension blipExtension = new BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; UseLocalDpi useLocalDpi = new UseLocalDpi() { Val = false }; useLocalDpi.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); blipExtension.Append(useLocalDpi); blipExtensionList.Append(blipExtension); Blip blip = new Blip() { Embed = embedId }; blip.Append(blipExtensionList); Stretch stretch = new Stretch(); FillRectangle fillRectangle = new FillRectangle(); stretch.Append(fillRectangle); blipFill.Append(blip); blipFill.Append(stretch); // TODO calc the size A.Transform2D transform2D = new A.Transform2D(); A.Offset offset = new A.Offset() { X = 457200L, Y = 1524000L }; A.Extents extents = new A.Extents() { Cx = 8229600L, Cy = 5029200L }; 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); P.ShapeProperties shapeProperties = new P.ShapeProperties(); shapeProperties.Append(transform2D); shapeProperties.Append(presetGeometry); picture.Append(nonVisualPictureProperties); picture.Append(blipFill); picture.Append(shapeProperties); slidePart.Slide.CommonSlideData.ShapeTree.AppendChild(picture); var ext = System.IO.Path.GetExtension(imagePath).Substring(1); ext = ext.Equals("png", StringComparison.OrdinalIgnoreCase) ? "image/png" : "image/jpeg"; ImagePart imagePart = slidePart.AddNewPart <ImagePart>(ext, embedId); using (FileStream fileStream = new FileStream(imagePath, FileMode.Open)) { imagePart.FeedData(fileStream); } }