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); }
public static string[] GetAllTextInSlide(DocumentFormat.OpenXml.Packaging.SlidePart slidePart) { // Verify that the slide part exists. if (slidePart == null) { throw new System.ArgumentNullException("slidePart"); } // Create a new linked list of strings. System.Collections.Generic.LinkedList <string> texts = new System.Collections.Generic.LinkedList <string>(); // If the slide exists... if (slidePart.Slide != null) { // Iterate through all the paragraphs in the slide. foreach (DocumentFormat.OpenXml.Drawing.Paragraph paragraph in slidePart.Slide.Descendants <DocumentFormat.OpenXml.Drawing.Paragraph>()) { // Create a new string builder. System.Text.StringBuilder paragraphText = new System.Text.StringBuilder(); // Iterate through the lines of the paragraph. foreach (DocumentFormat.OpenXml.Drawing.Text text in paragraph.Descendants <DocumentFormat.OpenXml.Drawing.Text>()) { // Append each line to the previous lines. paragraphText.Append(text.Text); } if (paragraphText.Length > 0) { // Add each paragraph to the linked list. texts.AddLast(paragraphText.ToString()); } } } if (texts.Count > 0) { // Return an array of strings. return(System.Linq.Enumerable.ToArray(texts)); } else { return(null); } }
// Get the title string of the slide. public static string GetSlideTitle(DocumentFormat.OpenXml.Packaging.SlidePart slidePart) { if (slidePart == null) { throw new System.ArgumentNullException("presentationDocument"); } // Declare a paragraph separator. string paragraphSeparator = null; if (slidePart.Slide != null) { // Find all the title shapes. var shapes = from shape in slidePart.Slide.Descendants <DocumentFormat.OpenXml.Presentation.Shape>() where IsTitleShape(shape) select shape; System.Text.StringBuilder paragraphText = new System.Text.StringBuilder(); foreach (var shape in shapes) { // Get the text in each paragraph in this shape. foreach (var paragraph in shape.TextBody.Descendants <DocumentFormat.OpenXml.Drawing.Paragraph>()) { // Add a line break. paragraphText.Append(paragraphSeparator); foreach (var text in paragraph.Descendants <DocumentFormat.OpenXml.Drawing.Text>()) { paragraphText.Append(text.Text); } paragraphSeparator = "\n"; } } return(paragraphText.ToString()); } return(string.Empty); }
// 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); }