Exemplo n.º 1
0
        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();
        }
        /// <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();
        }
Exemplo n.º 3
0
        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);
            }
        }