首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在形状pptx (Spire.Presentation)中查找文本

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

Stack Overflow用户
提问于 2019-02-27 19:21:32
回答 1查看 386关注 0票数 0

面临着在演示文稿中更改文本的问题。我使用Spire.Presentation,页面上有很多不同的形状。我的版本只找到10个文本中的1个。如何更改Shapesi才能获得所有文本

代码语言:javascript
复制
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);
                }
            }
EN

回答 1

Stack Overflow用户

发布于 2019-02-27 20:16:21

看起来您正在循环浏览幻灯片,但并没有循环浏览幻灯片上的所有形状。这段代码将需要

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

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

  • 第三个slide

  • ...

的第三个形状

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

代码语言:javascript
复制
    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);
                    }
                  }
                }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54904319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档