(一) Interop Assembly
.NET Frameword中提供了一种叫COM Interop的技术,用于在.NET代码中直接访问COM组件。原
理是.NET Framework能够自动针对一个COM组件,帮助开发人员生成一个Interop Assembly(IA)
,IA是一个托管程序集,里面的命名空间,类,方法等都和所针对的COM对应的(并非都一一对
应,是按照一定规则转换),在程序中可以添加对应COM组件的IA引用就可实现对COM的操作。
(二) Primary Interop Assembly
IA在添加一个COM引用时自动生成,对于Office,微软提供了现成的IA供程序员使用,称为,
Primary Interop Assembly(PIA),下面是和主题相关的一些PIAS(Office 2003)
我们引入PIA之后就可对PPT进行操作,对于PPT.文本内容存在于PPT中的
TextFrame,其中TextFrame可以包含TextRange.而TextRange中的文本正是我们所需要的,关于PPT对象模型可参考MSDN
1) 转换为纯文本(txt)教案
观察powerpoint的对象模型,对于我们提取文本相关的是TextFrame,其中TextFrame可以包含TextRange.而TextRange中的文本正是我们所需要的
protected void ConvertPptToText(string inputfilename)
{
//定义一个Powerpoint对象
Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
//打开指定的ppt
Microsoft.Office.Interop.PowerPoint.Presentation ppt = pptApp.Presentations.Open(filename, Microsoft.Office.Core.MsoTriState.msoCTrue,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse);
StringBuilder sb = new StringBuilder();
foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in ppt.Slides)
{//对于每一张幻灯片中的每个shape
foreach (Microsoft.Office.Interop.PowerPoint.Shape shape in slide.Shapes)
{
//若包含TextFrame且TextFrame中包含文本
if ((shape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue) && (shape.TextFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue))
{
//则追加文本
sb.AppendLine(shape.TextFrame.TextRange.Text);//获取文本
}
}
}
ppt.Close();//关闭所打开的ppt
pptApp.Quit();
File.WriteAllText(@"E:/c#/pptToText.txt", sb.ToString());//写入到文件中