如何在形状pptx中查找文本(Spire.Presentation)

时间:2019-02-27 11:21:32

标签: c# .net spire

解决了更改演示文稿中文本的问题。我使用Spire.Presentation,页面上有很多不同的形状。我的版本仅找到10个文本中的1个。如何更改Shapes [i]以获取所有文本

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 个答案:

答案 0 :(得分:0)

看起来您正在遍历幻灯片,但未遍历幻灯片上的所有“形状”。 该代码将采用

  • 第一张幻灯片的第一张形状

  • 第二张幻灯片的第二个形状

  • 第三张幻灯片的第三种形状
  • ...

我认为您的解决方案是还要遍历每一页中的所有Shape,就像这样:

    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);
                    }
                  }
                }