/// <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(); }
private GeometryType ForCompositeElement(OpenXmlCompositeElement sdkCompositeElement, P.ShapeProperties spPr) { var t2D = spPr.Transform2D; if (t2D != null) { var presetGeometry = spPr.GetFirstChild <PresetGeometry>(); // Placeholder can have transform on the slide, without having geometry if (presetGeometry == null) { if (spPr.OfType <CustomGeometry>().Any()) { return(GeometryType.Custom); } return(FromLayout()); } var name = presetGeometry.Preset.Value.ToString(); Enum.TryParse(name, true, out GeometryType geometryType); return(geometryType); } return(FromLayout()); GeometryType FromLayout() { var placeholderLocationData = _phService.TryGet(sdkCompositeElement); if (placeholderLocationData == null) { return(GeometryType.Rectangle); } return(placeholderLocationData.Geometry); } }
private void insertLink(Slide slide, PageUrl pageUrl, int objId) { slide.SlidePart.AddHyperlinkRelationship(new System.Uri(pageUrl.url, System.UriKind.Absolute), true, "rId" + objId); P.Shape shape = new P.Shape(); P.NonVisualShapeProperties nonVisualShapeProperties1 = new P.NonVisualShapeProperties() { NonVisualDrawingProperties = new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "矩形 1", HyperlinkOnClick = new A.HyperlinkOnClick() { Id = "rId" + objId } }, NonVisualShapeDrawingProperties = new P.NonVisualShapeDrawingProperties(), ApplicationNonVisualDrawingProperties = new P.ApplicationNonVisualDrawingProperties() }; P.ShapeProperties shapeProperties = new P.ShapeProperties() { Transform2D = new A.Transform2D() { Offset = new A.Offset() { X = pageUrl.origin.Width * ImageInfoUtils.RATE, Y = pageUrl.origin.Height * ImageInfoUtils.RATE }, Extents = new A.Extents() { Cx = pageUrl.size.Width * ImageInfoUtils.RATE, Cy = pageUrl.size.Height * ImageInfoUtils.RATE } } }; A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle, AdjustValueList = new A.AdjustValueList() }; A.NoFill noFill = new A.NoFill(); A.Outline outline = new A.Outline(); A.NoFill noFill2 = new A.NoFill(); outline.Append(noFill2); shapeProperties.Append(presetGeometry); shapeProperties.Append(noFill); shapeProperties.Append(outline); P.ShapeStyle shapeStyle1 = new P.ShapeStyle(); A.LineReference lineReference = new A.LineReference() { Index = (UInt32Value)2U }; A.SchemeColor schemeColor = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 }; A.Shade shade1 = new A.Shade() { Val = 50000 }; schemeColor.Append(shade1); lineReference.Append(schemeColor); A.FillReference fillReference = new A.FillReference() { Index = (UInt32Value)1U }; A.SchemeColor schemeColor2 = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 }; fillReference.Append(schemeColor2); A.EffectReference effectReference = new A.EffectReference() { Index = (UInt32Value)0U }; A.SchemeColor schemeColor3 = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 }; effectReference.Append(schemeColor3); A.FontReference fontReference = new A.FontReference() { Index = A.FontCollectionIndexValues.Minor }; A.SchemeColor schemeColor4 = new A.SchemeColor() { Val = A.SchemeColorValues.Light1 }; fontReference.Append(schemeColor4); shapeStyle1.Append(lineReference); shapeStyle1.Append(fillReference); shapeStyle1.Append(effectReference); shapeStyle1.Append(fontReference); P.TextBody textBody = new P.TextBody(); A.BodyProperties bodyProperties = new A.BodyProperties() { RightToLeftColumns = false, Anchor = A.TextAnchoringTypeValues.Center }; A.ListStyle listStyle = new A.ListStyle(); A.Paragraph paragraph = new A.Paragraph(); A.ParagraphProperties paragraphProperties = new A.ParagraphProperties() { Alignment = A.TextAlignmentTypeValues.Center }; A.EndParagraphRunProperties endParagraphRunProperties = new A.EndParagraphRunProperties() { Language = "zh-CN", AlternativeLanguage = "en-US" }; paragraph.Append(paragraphProperties); paragraph.Append(endParagraphRunProperties); textBody.Append(bodyProperties); textBody.Append(listStyle); textBody.Append(paragraph); shape.Append(nonVisualShapeProperties1); shape.Append(shapeProperties); shape.Append(shapeStyle1); shape.Append(textBody); slide.CommonSlideData.ShapeTree.AppendChild(shape); }
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); } }