0

Faced the problem of changing the text in the presentation. I use Spire.Presentation and there are a lot of different shapes on the page. My version finds only 1 text of 10. how to change Shapes[i] that i can get all the texts

using Spire.Presentation;
using System;
using System.Linq;
using System.Collections.Generic;
    static void Main(string[] args)
            {
                Presentation presentation = new Presentation();
                //Open presentation and convert slides
                presentation.LoadFromFile(@"C:\input.pptx");
                //if (presentation == null) { return };
                List<string> texts = new List<string>();
                for (int i = 0; i < presentation.Slides.Count; i++)
                {
                    //Get the shape from slide, get the text from shape and save to a new string variable.
                    IAutoShape shape = presentation.Slides[i].Shapes[i] as IAutoShape;IAutoShape shape = presentation.Slides[i].Shapes.GetEnumerator() as IAutoShape;
            if (shape != null)
            {
                foreach (var s in shape.ToString())
                {
                    var originalText = shape.TextFrame.TextRange;
                    originalText.FontHeight = 12;
                    originalText.IsItalic = TriState.True;
                    originalText.TextUnderlineType = TextUnderlineType.Single;
                    originalText.LatinFont = new TextFont("Arial");
                }
            }
            Console.WriteLine(shape);
            Console.ReadKey();
                    //save the slide to Image
                    var image = presentation.Slides[i].SaveAsImage();
                    String fileName = String.Format(@"C:\img-{0}.png", i);
                    image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                }
            }

1 Answers1

0

Looks like you are looping through your slides but aren't looping through all the Shapes on your slide. This code will take

  • the first shape of the first slide

  • the second shape of the second slide

  • the third shape of the third slide
  • ...

I think your solution is to also loop through all the Shapes in each page, like this:

    using Spire.Presentation;
    using System;
    using System.Linq;
    using System.Collections.Generic;
        static void Main(string[] args)
                {
                    Presentation presentation = new Presentation();
                    //Open presentation and convert slides
                    presentation.LoadFromFile(@"C:\input.pptx");
                    //if (presentation == null) { return };
                    List<string> texts = new List<string>();
                    for (int i = 0; i < presentation.Slides.Count; i++)
                    {
                      for(int j = 0; j < presentation.Slides[i].Shapes.Count;j++)
                      {
                        //Get the shape from slide, get the text from shape and save to a new string variable.
                        IAutoShape shape = presentation.Slides[i].Shapes[j] as IAutoShape;IAutoShape shape = presentation.Slides[i].Shapes.GetEnumerator() as IAutoShape;
                if (shape != null)
                {
                    foreach (var s in shape.ToString())
                    {
                        var originalText = shape.TextFrame.TextRange;
                        originalText.FontHeight = 12;
                        originalText.IsItalic = TriState.True;
                        originalText.TextUnderlineType = TextUnderlineType.Single;
                        originalText.LatinFont = new TextFont("Arial");
                    }
                }
                Console.WriteLine(shape);
                Console.ReadKey();
                        //save the slide to Image
                        var image = presentation.Slides[i].SaveAsImage();
                        String fileName = String.Format(@"C:\img-{0}.png", i);
                        image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                    }
                  }
                }
DenseCrab
  • 1,273
  • 11
  • 22