- Code: Select all
/// <summary>
/// 获取PPT中的文字
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
private static string ReadPptText(string file)
{
var presentation = new Presentation();
presentation.LoadFromFile(file);
var allText = new StringBuilder();
foreach (ISlide slide in presentation.Slides)
{
foreach (IShape shape in slide.Shapes)
{
switch (shape)
{
// 检查形状是否包含文本
case IAutoShape { TextFrame: not null } autoShape:
{
// 提取文本
foreach (TextParagraph paragraph in autoShape.TextFrame.Paragraphs)
{
foreach (Spire.Presentation.TextRange textRange in paragraph.TextRanges)
{
allText.Append(textRange.Text);
}
}
break;
}
// 获取表格中的文字
case ITable table:
{
for (var i = 0; i < table.TableRows.Count; i++)
{
for (var j = 0; j < table.ColumnsList.Count; j++)
{
Spire.Presentation.Cell cell = table.TableRows[i][j];
foreach (Spire.Presentation.TextParagraph paragraph in cell.TextFrame.Paragraphs)
{
foreach (Spire.Presentation.TextRange textRange in paragraph.TextRanges)
{
allText.Append(textRange.Text);
}
}
}
}
break;
}
case ISmartArt smartArt:
{
for (var i = 0; i < smartArt.Nodes.Count; i++)
{
allText.Append(smartArt.Nodes[i].TextFrame.Text);
}
break;
}
}
}
// PPT换页则换行
allText.AppendLine();
}
presentation.Dispose();
return allText.ToString();
}