Esempio n. 1
0
        static void Main(string[] args)
        {
            IDictionary <string, string> toReplace = new Dictionary <string, string>();

            toReplace.Add("Sample2", "Sample - Text!");
            toReplace.Add("Sample3", "Sample 3 - Text!");

            using (PresentationDocument presentationDocument = PresentationDocument.Open("C:\\Users\\beggers\\Desktop\\ppt.pptx", true))
            {
                // Get the presentation part of the presentation document.
                PresentationPart presentationPart = presentationDocument.PresentationPart;

                // Verify that the presentation part and presentation exist.
                if (presentationPart != null && presentationPart.Presentation != null)
                {
                    // Get the Presentation object from the presentation part.
                    Presentation presentation = presentationPart.Presentation;

                    // Verify that the slide ID list exists.
                    if (presentation.SlideIdList != null)
                    {
                        foreach (var slideId in presentation.SlideIdList.Elements <SlideId>())
                        {
                            SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;

                            ShapeTree tree = slidePart.Slide.CommonSlideData.ShapeTree;
                            foreach (DocumentFormat.OpenXml.Presentation.Shape shape in tree.Elements <DocumentFormat.OpenXml.Presentation.Shape>())
                            {
                                // Run through all the paragraphs in the document
                                foreach (Paragraph paragraph in shape.Descendants().OfType <Paragraph>())
                                {
                                    foreach (DocumentFormat.OpenXml.Drawing.Run run in paragraph.Elements <Run>())
                                    {
                                        foreach (var kvp in toReplace)
                                        {
                                            if (run.Text.InnerText.Contains(kvp.Key))
                                            {
                                                run.Text = new DocumentFormat.OpenXml.Drawing.Text(kvp.Value);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private void setStringValues(SlidePart sectionSlidePart)
        {
            foreach (var item in sectionSlidePart.Slide.Descendants())
            {
                if (item.GetType() == typeof(DocumentFormat.OpenXml.Presentation.Shape))
                {
                    foreach (Paragraph paragraph in item.Descendants().OfType <Paragraph>())
                    {
                        setRunData(paragraph);
                    }
                }

                if (item.GetType() == typeof(ShapeTree))
                {
                    foreach (DocumentFormat.OpenXml.Presentation.Shape shape in item.Elements <DocumentFormat.OpenXml.Presentation.Shape>())
                    {
                        foreach (Paragraph paragraph in shape.Descendants().OfType <Paragraph>())
                        {
                            setRunData(paragraph);
                        }
                    }
                }
                if (item.GetType() == typeof(CommonSlideData))
                {
                    CommonSlideData sldData1 = (CommonSlideData)item;

                    ShapeTree tree = sldData1.ShapeTree;
                    foreach (DocumentFormat.OpenXml.Presentation.Shape shape in tree.Elements <DocumentFormat.OpenXml.Presentation.Shape>())
                    {
                        foreach (Paragraph paragraph in shape.Descendants().OfType <Paragraph>())
                        {
                            setRunData(paragraph);
                        }
                    }

                    foreach (DocumentFormat.OpenXml.Presentation.GroupShape groupShape in tree.Elements <DocumentFormat.OpenXml.Presentation.GroupShape>())
                    {
                        foreach (DocumentFormat.OpenXml.Presentation.Shape shapeinner in groupShape.Elements <DocumentFormat.OpenXml.Presentation.Shape>())
                        {
                            foreach (Paragraph paragraph in shapeinner.Descendants().OfType <Paragraph>())
                            {
                                setRunData(paragraph);
                            }
                        }

                        foreach (Paragraph paragraph in groupShape.Descendants().OfType <Paragraph>())
                        {
                            setRunData(paragraph);
                        }
                    }

                    foreach (DocumentFormat.OpenXml.Presentation.GraphicFrame shape in tree.Elements <DocumentFormat.OpenXml.Presentation.GraphicFrame>())
                    {
                        foreach (Paragraph paragraph in shape.Descendants().OfType <Paragraph>())
                        {
                            setRunData(paragraph);
                        }
                    }

                    foreach (DocumentFormat.OpenXml.Presentation.ConnectionShape shape in tree.Elements <DocumentFormat.OpenXml.Presentation.ConnectionShape>())
                    {
                        foreach (Paragraph paragraph in shape.Descendants().OfType <Paragraph>())
                        {
                            setRunData(paragraph);
                        }
                    }

                    sectionSlidePart.Slide.Save();
                }
            }
        }
Esempio n. 3
0
        public void SetPPTShapeText(string[] arrayParameters, int numSlide)
        {
            using (PresentationDocument ppt = PresentationDocument.Open(HttpContext.Current.Server.MapPath(folder), true))
            {
                numSlide--;
                IEnumerable <Shape> elementSlide;
                PresentationPart    part     = ppt.PresentationPart;
                OpenXmlElementList  slideIds = part.Presentation.SlideIdList.ChildElements;
                string relId = (slideIds[numSlide] as SlideId).RelationshipId;

                SlidePart slide = (SlidePart)part.GetPartById(relId);
                if (slide != null)
                {
                    ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;

                    elementSlide = tree.Elements <Shape>();

                    foreach (Shape shape1 in elementSlide)
                    {
                        string valueStr = shape1.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value;
                        switch (valueStr)
                        {
                        case "Заголовок 1":
                            Drawing.Paragraph paragraph1 = shape1.TextBody.ChildElements.OfType <Drawing.Paragraph>().ElementAt(0);
                            Drawing.Run       run1       = paragraph1.ChildElements.OfType <Drawing.Run>().ElementAt(0);
                            run1.Text.Text = arrayParameters[0];
                            slide.Slide.Save();
                            break;

                        case "Прямоугольник 4":
                            paragraph1     = shape1.TextBody.ChildElements.OfType <Drawing.Paragraph>().ElementAt(0);
                            run1           = paragraph1.ChildElements.OfType <Drawing.Run>().ElementAt(0);
                            run1.Text.Text = arrayParameters[1];
                            slide.Slide.Save();
                            break;

                        case "Прямоугольник 6":
                            paragraph1     = shape1.TextBody.ChildElements.OfType <Drawing.Paragraph>().ElementAt(0);
                            run1           = paragraph1.ChildElements.OfType <Drawing.Run>().ElementAt(0);
                            run1.Text.Text = arrayParameters[2];
                            slide.Slide.Save();
                            break;

                        case "Прямоугольник 9":
                            paragraph1     = shape1.TextBody.ChildElements.OfType <Drawing.Paragraph>().ElementAt(0);
                            run1           = paragraph1.ChildElements.OfType <Drawing.Run>().ElementAt(0);
                            run1.Text.Text = arrayParameters[3];
                            slide.Slide.Save();
                            break;

                        case "Прямоугольник 11":
                            paragraph1     = shape1.TextBody.ChildElements.OfType <Drawing.Paragraph>().ElementAt(0);
                            run1           = paragraph1.ChildElements.OfType <Drawing.Run>().ElementAt(0);
                            run1.Text.Text = arrayParameters[4];
                            slide.Slide.Save();
                            break;

                        case "Прямоугольник 13":
                            paragraph1     = shape1.TextBody.ChildElements.OfType <Drawing.Paragraph>().ElementAt(0);
                            run1           = paragraph1.ChildElements.OfType <Drawing.Run>().ElementAt(0);
                            run1.Text.Text = arrayParameters[5];
                            slide.Slide.Save();
                            break;

                        case "Прямоугольник 18":
                            paragraph1     = shape1.TextBody.ChildElements.OfType <Drawing.Paragraph>().ElementAt(0);
                            run1           = paragraph1.ChildElements.OfType <Drawing.Run>().ElementAt(0);
                            run1.Text.Text = arrayParameters[6];
                            slide.Slide.Save();
                            break;

                        case "Прямоугольник 20":
                            paragraph1     = shape1.TextBody.ChildElements.OfType <Drawing.Paragraph>().ElementAt(0);
                            run1           = paragraph1.ChildElements.OfType <Drawing.Run>().ElementAt(0);
                            run1.Text.Text = arrayParameters[7];
                            slide.Slide.Save();
                            break;
                        }
                    }
                }
                ppt.Close();
            }
        }