Пример #1
0
        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();
        }
Пример #2
0
        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.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);
            }
        }
Пример #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);
            }
        }
        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();
        }