/// <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();
        }
Exemple #2
0
        /// <summary>
        /// Lấy màu image background Slidemaster
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="openXmlPart"></param>
        /// <param name="blipFill"></param>
        /// <returns></returns>
        private ImageColor GetImageColor(pp.FillFormat fill, OpenXmlPart openXmlPart, DocumentFormat.OpenXml.Presentation.BlipFill blipFill)
        {
            ImagePart part = null;
            IEnumerable <DocumentFormat.OpenXml.Presentation.Picture> lstPic = null;

            if (openXmlPart is SlidePart slidePart)
            {
                lstPic = slidePart.Slide.Descendants <DocumentFormat.OpenXml.Presentation.Picture>().Select(p => p);
                if (lstPic != null)
                {
                    string rID = blipFill.Blip.Embed.Value;
                    part = (ImagePart)slidePart.GetPartById(rID);
                }
            }
            else if (openXmlPart is SlideMasterPart slideMasterPart)
            {
                lstPic = slideMasterPart.SlideMaster.Descendants <DocumentFormat.OpenXml.Presentation.Picture>().Select(p => p);
                if (lstPic != null)
                {
                    string rID = blipFill.Blip.Embed.Value;
                    part = (ImagePart)slideMasterPart.GetPartById(rID);
                }
            }
            else if (openXmlPart is SlideLayoutPart slideLayoutPart)
            {
                lstPic = slideLayoutPart.SlideLayout.Descendants <DocumentFormat.OpenXml.Presentation.Picture>().Select(p => p);
                if (lstPic != null)
                {
                    string rID = blipFill.Blip.Embed.Value;
                    part = (ImagePart)slideLayoutPart.GetPartById(rID);
                }
            }
            if (lstPic != null && part != null)
            {
                string pathImage = System.IO.Path.Combine((System.Windows.Application.Current as IAppGlobal).DocumentTempFolder, ObjectElementsHelper.RandomString(13) + System.IO.Path.GetExtension(part.Uri.ToString()));
                Utils.CopyStream(part.GetStream(), pathImage);
                ImageColor imageColor = new ImageColor();
                imageColor.Source           = new Core.Model.Image(pathImage);
                imageColor.Opacity          = 1 - fill.Transparency;
                imageColor.OffsetX          = fill.TextureOffsetX;
                imageColor.OffsetY          = fill.TextureOffsetY;
                imageColor.IsTiled          = fill.TextureTile == MsoTriState.msoTrue;
                imageColor.Alignment        = ConvertImageAligment(fill.TextureAlignment);
                imageColor.ScaleX           = fill.TextureHorizontalScale;
                imageColor.ScaleY           = fill.TextureVerticalScale;
                imageColor.RotateWidthShape = fill.RotateWithObject == MsoTriState.msoTrue;
                foreach (var item in blipFill)
                {
                    if (item is Stretch stretch)
                    {
                        if (stretch.FillRectangle.Top != null)
                        {
                            imageColor.OffsetTop = stretch.FillRectangle.Top / 100000;
                        }
                        if (stretch.FillRectangle.Left != null)
                        {
                            imageColor.OffsetLeft = stretch.FillRectangle.Left / 100000;
                        }
                        if (stretch.FillRectangle.Right != null)
                        {
                            imageColor.OffsetRight = stretch.FillRectangle.Right / 100000;
                        }
                        if (stretch.FillRectangle.Bottom != null)
                        {
                            imageColor.OffsetBottom = stretch.FillRectangle.Bottom / 100000;
                        }
                    }
                }
                return(imageColor);
            }
            return(null);
        }
        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);
            }
        }
Exemple #4
0
        /// <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);
        }
Exemple #5
0
        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);
                }
            }
        }
Exemple #6
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();
        }
        // Replace image in PPT shape.
        public void ReplaceFirstImageMatchingAltText(PresentationDocument presentationDocument, string newImage, string alternateTextToFind,
                                                     string newImagePath, List <string> LstShowPicture, List <string> LstShowText,
                                                     int SlideIndex, DataTable dtFieldActive, List <PPTXTemplateFields> PPTXAllparams)
        {
            try
            {
                int Idx = SlideIndex * 1000, Idz = 0, TextIndex = 0;
                OpenXmlElementList slideIds = presentationDocument.PresentationPart.Presentation.SlideIdList.ChildElements;

                foreach (SlideId sID in slideIds) // loop thru the SlideIDList
                {
                    if (Idz++ == SlideIndex)
                    {
                        string    relId = sID.RelationshipId;                                                  // get first slide relationship
                        SlidePart slide = (SlidePart)presentationDocument.PresentationPart.GetPartById(relId); // Get the slide part from the relationship ID.

                        var pictures = slide.Slide.Descendants <P.ShapeTree>().First().Descendants <P.Picture>().ToList();
                        foreach (var picture in pictures)
                        {
                            // get photo desc to see if it matches search text
                            var nonVisualPictureProperties = picture.Descendants <P.NonVisualPictureProperties>().FirstOrDefault();
                            if (nonVisualPictureProperties == null)
                            {
                                continue;
                            }
                            var nonVisualDrawingProperties99 = nonVisualPictureProperties.Descendants <P.NonVisualDrawingProperties>().FirstOrDefault();
                            if (nonVisualDrawingProperties99 == null)
                            {
                                continue;
                            }
                            var desc = nonVisualDrawingProperties99.Description;
                            //if (desc == null || desc.Value == null || !desc.Value.Contains(alternateTextToFind))
                            //    continue;

                            P.BlipFill blipFill = picture.Descendants <P.BlipFill>().First();
                            var        blip     = blipFill.Descendants <DocumentFormat.OpenXml.Drawing.Blip>().First();
                            string     embedId  = blip.Embed; // now we need to find the embedded content and update it.

                            // find the content
                            ImagePart imagePart = (ImagePart)slide.GetPartById(embedId);
                            if (imagePart != null)
                            {
                                try
                                {
                                    using (FileStream fileStream = new FileStream(newImagePath + LstShowPicture[Convert.ToInt32(desc)], FileMode.Open))
                                    {
                                        imagePart.FeedData(fileStream);
                                        fileStream.Close();
                                    }
                                }
                                catch (Exception ex)
                                {
                                    try
                                    {
                                        using (FileStream fileStream = new FileStream(newImagePath + newImage, FileMode.Open))
                                        {
                                            imagePart.FeedData(fileStream);
                                            fileStream.Close();
                                        }
                                    }
                                    catch (Exception ex1)
                                    {
                                    }
                                }
                            }
                        }

                        // String Text
                        foreach (string StringText in LstShowText)
                        {
                            string[] Value = StringText.Split(';');
                            List <DocumentFormat.OpenXml.Drawing.Text> textList = slide.Slide.Descendants <DocumentFormat.OpenXml.Drawing.Text>().ToList();
                            foreach (DocumentFormat.OpenXml.Drawing.Text txt in textList)
                            {
                                foreach (PPTXTemplateFields pptx in PPTXAllparams)
                                {
                                    string SelectedField = pptx.SelectedField.Trim().Substring(0, pptx.SelectedField.Trim().Length - 1);
                                    try
                                    {
                                        if (txt.Text.Contains(SelectedField + (TextIndex + 1).ToString()))
                                        {
                                            if (pptx.FieldDataType == "String" || pptx.FieldDataType == "Int")
                                            {
                                                int Index = FieldPosition(dtFieldActive, pptx.MappedFields);
                                                //if (Index != -1) txt.Text = txt.Text.Replace(SelectedField + (TextIndex + 1).ToString(), Value[Index]); else txt.Text = " ";
                                                if (Index != -1)
                                                {
                                                    txt.Text = txt.Text.SafeReplace(SelectedField + (TextIndex + 1).ToString(), Value[Index], true);
                                                }
                                                else
                                                {
                                                    txt.Text = " ";
                                                }
                                            }
                                            else if (pptx.FieldDataType == "Float")
                                            {
                                                int Index = FieldPosition(dtFieldActive, pptx.MappedFields);
                                                if (Index != -1)
                                                {
                                                    try
                                                    {
                                                        //txt.Text = txt.Text.Replace(SelectedField + (TextIndex + 1).ToString(), Convert.ToDecimal(Value[Index]).ToString("#,##0.00"));
                                                        txt.Text = txt.Text.SafeReplace(SelectedField + (TextIndex + 1).ToString(), Convert.ToDecimal(Value[Index]).ToString("#,##0.00"), true);
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        txt.Text = " ";
                                                    }
                                                }
                                                else
                                                {
                                                    txt.Text = " ";
                                                }
                                            }
                                            else if (pptx.FieldDataType == "DateTime")
                                            {
                                                int Index = FieldPosition(dtFieldActive, pptx.MappedFields);
                                                if (Index != -1)
                                                {
                                                    //txt.Text = txt.Text.Replace(SelectedField + (TextIndex + 1).ToString(), Convert.ToDateTime(Value[Index]).ToString(pptx.FieldDataFormat, CultureInfo.InvariantCulture).ToString());
                                                    txt.Text = txt.Text.SafeReplace(SelectedField + (TextIndex + 1).ToString(), Convert.ToDateTime(Value[Index]).ToString(pptx.FieldDataFormat, CultureInfo.InvariantCulture).ToString(), true);
                                                }
                                                else
                                                {
                                                    txt.Text = " ";
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                }
                            }
                            TextIndex++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
Exemple #8
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);
            }
        }