如何使用Aspose从C#中获取幻灯片中的文本

时间:2015-03-12 13:25:47

标签: c# asp.net-mvc-4 aspose-slides

我在ppt文件的幻灯片中获得所有形状现在我想从这些形状中获取文本我该怎么做

这是我的方法,我在ppt文件中获取所有幻灯片的形状

public void Main(string[] args)
    {
        // The path to the documents directory.
        string dataDir = Path.GetFullPath(@"C:\Users\Vipin\Desktop\");
        //Load the desired the presentation
        Presentation pres = new Presentation(dataDir + "Android.ppt");
        using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
        {


            //Accessing a slide using its slide index
            int slideCount = prestg.Slides.Count();
            for (int i = 0; i <= slideCount - 1; i++)
            {
                ISlide slide = pres.Slides[i];
                foreach (IShape shap in slide.Shapes)
                {
                    int slideCountNumber = i + 1;

                    float shapeHeight = shap.Frame.Height;
                    float shapeWidth = shap.Frame.Width;
                    Debug.Write("slide Number: " + slideCountNumber + " shape width = " + shapeWidth + " shapeHeight = " + shapeHeight);

                }
            }

        }
    }

现在可以从中获取文字

2 个答案:

答案 0 :(得分:0)

您可能希望不是从所有形状中提取文本,而是从文本框架中提取文本。为此,请使用GetAllTextFrames

公开的PresentationScanner静态方法
using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
{
   //Get an Array of ITextFrame objects from all slides in the PPTX
   ITextFrame[] textFramesPPTX = Aspose.Slides.Util.SlideUtil.GetAllTextFrames(pptxPresentation, true);

   //Loop through the Array of TextFrames
   for (int i = 0; i < textFramesPPTX.Length; i++)

   //Loop through paragraphs in current ITextFrame
   foreach (IParagraph para in textFramesPPTX[i].Paragraphs)

       //Loop through portions in the current IParagraph
       foreach (IPortion port in para.Portions)
       {
           //Display text in the current portion
           Console.WriteLine(port.Text);

           //Display font height of the text
           Console.WriteLine(port.PortionFormat.FontHeight);

           //Display font name of the text
           if (port.PortionFormat.LatinFont != null)
               Console.WriteLine(port.PortionFormat.LatinFont.FontName);
       }

请参阅documentation

答案 1 :(得分:0)

如果你没有它的许可证,aspose将给你截断的文本。所以如果你使用Microsoft.Office.Interop.PowerPoint

,对你来说会更好

使用如下

public void ReadSlide(){

        string filePath= @"C:\Users\UserName\Slide.pptx";

        Microsoft.Office.Interop.PowerPoint.Application PowerPoint_App = new Microsoft.Office.Interop.PowerPoint.Application();
        Microsoft.Office.Interop.PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
        Microsoft.Office.Interop.PowerPoint.Presentation presentation = multi_presentations.Open(filePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);

        string presentation_textforParent = "";
        foreach (var item in presentation.Slides[1].Shapes)
        {
            var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;
            if (shape.HasTextFrame == MsoTriState.msoTrue)
            {
                if (shape.TextFrame.HasText == MsoTriState.msoTrue)
                {
                    var textRange = shape.TextFrame.TextRange;
                    var text = textRange.Text;

                    presentation_textforParent += text + " ";
                }
            }
        }

}