在图文混排的文档中,我们可以根据需要将文档中的文字信息或者图片提取出来,通过c#代码可以提取word和pdf文件中的文本和图片,那么同样的,我们也可以提取ppt幻灯片当中的文本和图片。本篇文档将讲述如何使用c#来实现提取ppt文本和图片的操作。首先也是需要安装组件spire.presentation,然后添加引用dll文件到项目中。下面是主要的代码步骤。
原文档:
1. 提取文本
步骤一:创建一个presentation实例并加载文档
presentation presentation = new presentation( @"c:\users\administrator\desktop\sample.pptx" , fileformat.pptx2010);
步骤二:创建一个stringbuilder对象
stringbuilder sb = new stringbuilder();
步骤三:遍历幻灯片及幻灯片中的图形,提取文本内容
foreach (islide slide in presentation.slides)
{
foreach (ishape shape in slide.shapes)
{
if (shape is iautoshape)
{
foreach (textparagraph tp in (shape as iautoshape).textframe.paragraphs)
{
sb.append(tp.text + environment.newline);
}
}
}
}
步骤四:写入txt文档
file.writealltext( "target.txt" , sb.tostring());
process.start( "target.txt" );
2. 提取图片
这里提取图片有两种情况,一种是提取整个文档中的所有图片,另外一种是只提取文档中某一特定幻灯片中的图片。
2.1提取所有图片
步骤一:初始化一个presentation类实例,并加载文档
presentation ppt = new presentation();
ppt.loadfromfile( @"c:\users\administrator\desktop\sample.pptx" );
步骤二:遍历文档中图片,提取图片并保存
for ( int i = 0; i < ppt.images.count; i++)
{
image image = ppt.images[i].image;
image.save( string .format( @"..\..\images{0}.png" , i));
}
提取的图片已保存到项目文件夹下
2.2.提取特定幻灯片中的图片
步骤一:创建一个presentation类实例,并加载文档
presentation ppt = new presentation();
ppt.loadfromfile( @"c:\users\administrator\desktop\sample.pptx" );
步骤二:获取第三张幻灯片,提取并保存图片
int i = 0;
foreach (ishape s in ppt.slides[2].shapes)
{
if (s is slidepicture)
{
slidepicture ps = s as slidepicture;
ps.picturefill.picture.embedimage.image.save( string .format( "{0}.png" , i));
i++;
}
if (s is pictureshape)
{
pictureshape ps = s as pictureshape;
ps.embedimage.image.save( string .format( "{0}.png" , i));
i++;
}
}
提取的第三张幻灯片中的图片已保存至指定位置
上文演示了如何提取文本和图片,步骤比较简单实用,希望对你有所帮助,感谢阅读!
原文链接:http://HdhCmsTestcnblogs测试数据/Yesi/archive/2017/11/02/7770802.html
dy("nrwz");
查看更多关于C#提取PPT文本和图片的实现方法的详细内容...