0

面临更改演示文稿中文本的问题。我使用 Spire.Presentation,页面上有很多不同的形状。我的版本仅找到 10 个文本。如何更改我可以获取所有文本的 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);
                }
            }
4

1 回答 1

0

看起来您正在循环播放幻灯片,但并未循环播放幻灯片上的所有形状。这段代码将采取

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

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

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

我认为您的解决方案是还遍历每个页面中的所有形状,如下所示:

    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);
                    }
                  }
                }
于 2019-02-27T12:16:21.307 回答