/// <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); }
//Insert Image into presentation public static void AddImage(string file, string image) { using (var presentation = PresentationDocument.Open(file, true)) { var slidePart = presentation .PresentationPart .SlideParts .First(); var part = slidePart .AddImagePart(ImagePartType.Jpeg); 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 = 2286000, Y = 1720840, }); picture.ShapeProperties.Transform2D.Append(new DocumentFormat.OpenXml.Drawing.Extents { Cx = 4572000, Cy = 3416320, } ); picture.ShapeProperties.Append(new DocumentFormat.OpenXml.Drawing.PresetGeometry { Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle }); tree.Append(picture); } }
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); } } }