public static void GetSlideIdAndText(out string sldText, string docName, int index) { using (DocumentFormat.OpenXml.Packaging.PresentationDocument ppt = DocumentFormat.OpenXml.Packaging.PresentationDocument.Open(docName, false)) { // Get the relationship ID of the first slide. DocumentFormat.OpenXml.Packaging.PresentationPart part = ppt.PresentationPart; DocumentFormat.OpenXml.OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; string relId = (slideIds[index] as DocumentFormat.OpenXml.Presentation.SlideId).RelationshipId; // Get the slide part from the relationship ID. DocumentFormat.OpenXml.Packaging.SlidePart slide = (DocumentFormat.OpenXml.Packaging.SlidePart)part.GetPartById(relId); // Build a StringBuilder object. System.Text.StringBuilder paragraphText = new System.Text.StringBuilder(); // Get the inner text of the slide: System.Collections.Generic.IEnumerable <DocumentFormat.OpenXml.Drawing.Text> texts = slide.Slide.Descendants <DocumentFormat.OpenXml.Drawing.Text>(); foreach (DocumentFormat.OpenXml.Drawing.Text text in texts) { paragraphText.Append(text.Text); } sldText = paragraphText.ToString(); } }
public static string[] GetAllTextInSlide(DocumentFormat.OpenXml.Packaging.PresentationDocument presentationDocument, int slideIndex) { // Verify that the presentation document exists. if (presentationDocument == null) { throw new System.ArgumentNullException("presentationDocument"); } // Verify that the slide index is not out of range. if (slideIndex < 0) { throw new System.ArgumentOutOfRangeException("slideIndex"); } // Get the presentation part of the presentation document. DocumentFormat.OpenXml.Packaging.PresentationPart presentationPart = presentationDocument.PresentationPart; // Verify that the presentation part and presentation exist. if (presentationPart != null && presentationPart.Presentation != null) { // Get the Presentation object from the presentation part. DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation; // Verify that the slide ID list exists. if (presentation.SlideIdList != null) { // Get the collection of slide IDs from the slide ID list. DocumentFormat.OpenXml.OpenXmlElementList slideIds = presentation.SlideIdList.ChildElements; // If the slide ID is in range... if (slideIndex < slideIds.Count) { // Get the relationship ID of the slide. string slidePartRelationshipId = (slideIds[slideIndex] as DocumentFormat.OpenXml.Presentation.SlideId).RelationshipId; // Get the specified slide part from the relationship ID. DocumentFormat.OpenXml.Packaging.SlidePart slidePart = (DocumentFormat.OpenXml.Packaging.SlidePart)presentationPart.GetPartById(slidePartRelationshipId); // Pass the slide part to the next method, and // then return the array of strings that method // returns to the previous method. return(GetAllTextInSlide(slidePart)); } } } // Else, return null. return(null); }
// Count the slides in the presentation. public static int CountSlides(DocumentFormat.OpenXml.Packaging.PresentationDocument presentationDocument) { // Check for a null document object. if (presentationDocument == null) { throw new System.ArgumentNullException("presentationDocument"); } int slidesCount = 0; // Get the presentation part of document. DocumentFormat.OpenXml.Packaging.PresentationPart presentationPart = presentationDocument.PresentationPart; // Get the slide count from the SlideParts. if (presentationPart != null) { slidesCount = System.Linq.Enumerable.Count(presentationPart.SlideParts); } // Return the slide count to the previous method. return(slidesCount); }
// Get a list of the titles of all the slides in the presentation. public static System.Collections.Generic.IList <string> GetSlideTitles(DocumentFormat.OpenXml.Packaging.PresentationDocument presentationDocument) { if (presentationDocument == null) { throw new System.ArgumentNullException("presentationDocument"); } // Get a PresentationPart object from the PresentationDocument object. DocumentFormat.OpenXml.Packaging.PresentationPart presentationPart = presentationDocument.PresentationPart; if (presentationPart != null && presentationPart.Presentation != null) { // Get a Presentation object from the PresentationPart object. DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation; if (presentation.SlideIdList != null) { System.Collections.Generic.List <string> titlesList = new System.Collections.Generic.List <string>(); // Get the title of each slide in the slide order. foreach (var slideId in presentation.SlideIdList.Elements <DocumentFormat.OpenXml.Presentation.SlideId>()) { DocumentFormat.OpenXml.Packaging.SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as DocumentFormat.OpenXml.Packaging.SlidePart; // Get the slide title. string title = GetSlideTitle(slidePart); // An empty title can also be added. titlesList.Add(title); } return(titlesList); } } return(null); }