Friday, 21 February 2014

How to read texts from microsoft PPTX document using Aspose.Slides,C# ,DotNet

Reading texts from Microsoft PPTX document using Aspose.Slides:

#region "Description"
/*  
    * Texts from each slide of Microsoft PPTX document can be extracted using GetAllTextsFromPPTX() method given below  
 */
#endregion

#region "Methods"

Method Name: GetAllTextsFromPPTX()
/// <summary>
/// Given Function takes input as PPTX document file stream and returns created text list for per page in PPTX
/// </summary>
/// <param name="SourceFileStream">PPTX doc file stream</param>
/// <returns>List of text per slide </returns>


public static Dictionary<int, string> GetAllTextsFromPPTX(Stream fileStream)
{
    Dictionary<int, string> pageContent = new Dictionary<int, string>();
    fileStream.Position = 0;
    using (Aspose.Slides.Pptx.PresentationEx pptxPresentation = new Aspose.Slides.Pptx.PresentationEx(fileStream))
    {
        //Get an Array of TextFrameEx objects from the first slide
        Aspose.Slides.Pptx.TextFrameEx[] textFramesSlideOne = null;
        //Loop through the Array of TextFrames
        for (int mSlide = 0; mSlide < pptxPresentation.Slides.Count; mSlide++)
        {
            textFramesSlideOne = Aspose.Slides.Util.SlideUtil.GetAllTextBoxes(pptxPresentation.Slides[mSlide]);
            if (!pageContent.ContainsKey(mSlide))
                pageContent.Add(mSlide, string.Empty);

            for (int i = 0; i < textFramesSlideOne.Length; i++)
            {
                //Loop through paragraphs in current TextFrame
                foreach (Aspose.Slides.Pptx.ParagraphEx para in textFramesSlideOne[i].Paragraphs)
                {
                    //Loop through portions in the current Paragraph
                    foreach (Aspose.Slides.Pptx.PortionEx port in para.Portions)
                    {
                        pageContent[mSlide] = pageContent[mSlide] + " ; " + port.Text;
                    }
                }
            }
        }
        return pageContent;
    }
}


No comments:

Post a Comment