1

i am getting all shapes in slides of ppt file now i want to get text from those shapes how can i do this

here is my method where i am getting shapes of all slides in ppt file

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);

                }
            }

        }
    }

now ho can i get the text from it

Neeraj Mehta
  • 1,675
  • 2
  • 22
  • 45

2 Answers2

1

aspose will give u truncated text if u don't have the license of it. so it will be better for you if you will use Microsoft.Office.Interop.PowerPoint

use as below

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 + " ";
                }
            }
        }

}

Vipswelt
  • 345
  • 1
  • 2
  • 6
  • What is presentation_textforParent? It looks like it's supposed to be the final return value, but why name it presentation_textforParent? – Caston Oct 27 '20 at 00:43
0

You may want to extract text not from all shapes, but from text frames instead. In order to do this use the GetAllTextFrames static method exposed by the PresentationScanner class

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);
       }

See documentation

Max Brodin
  • 3,903
  • 1
  • 14
  • 23
  • it showslike 'Click... text has been truncated due to evaluation version limitation' @Max Brodin – Neeraj Mehta Mar 13 '15 at 04:55
  • @NeerajMehta It means that Aspose libriary is not free and you have to buy it before use. – Max Brodin Mar 13 '15 at 06:27
  • is there any other method or library which is free to do this? – Neeraj Mehta Mar 13 '15 at 06:31
  • i have used another method for this by using DocumentFormat.OpenXml.Presentation; using DocumentFormat.OpenXml.Packaging; but getting error when opening a file http://stackoverflow.com/questions/29026179/getting-corrupted-file-error-while-trying-to-open-microsoft-powerpoint-file-usin – Neeraj Mehta Mar 13 '15 at 06:33