面临着在演示文稿中更改文本的问题。我使用Spire.Presentation,页面上有很多不同的形状。我的版本只找到10个文本中的1个。如何更改Shapesi才能获得所有文本
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);
}
}发布于 2019-02-27 20:16:21
看起来您正在循环浏览幻灯片,但并没有循环浏览幻灯片上的所有形状。这段代码将需要
第一个幻灯片的第一个形状
的第三个形状
我认为你的解决方案是循环遍历每个页面中的所有形状,如下所示:
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);
}
}
}https://stackoverflow.com/questions/54904319
复制相似问题