// Returns all the external hyperlinks in the slides of a presentation. public static System.Collections.Generic.IEnumerable <string> GetAllExternalHyperlinksInPresentation(string fileName) { // Declare a list of strings. System.Collections.Generic.List <string> ret = new System.Collections.Generic.List <string>(); // Open the presentation file as read-only. using (DocumentFormat.OpenXml.Packaging.PresentationDocument document = DocumentFormat.OpenXml.Packaging.PresentationDocument.Open(fileName, false)) { // Iterate through all the slide parts in the presentation part. foreach (DocumentFormat.OpenXml.Packaging.SlidePart slidePart in document.PresentationPart.SlideParts) { System.Collections.Generic.IEnumerable <DocumentFormat.OpenXml.Drawing.HyperlinkType> links = slidePart.Slide.Descendants <DocumentFormat.OpenXml.Drawing.HyperlinkType>(); // Iterate through all the links in the slide part. foreach (DocumentFormat.OpenXml.Drawing.HyperlinkType link in links) { // Iterate through all the external relationships in the slide part. foreach (DocumentFormat.OpenXml.Packaging.HyperlinkRelationship relation in slidePart.HyperlinkRelationships) { // If the relationship ID matches the link ID… if (relation.Id.Equals(link.Id)) { // Add the URI of the external relationship to the list of strings. ret.Add(relation.Uri.AbsoluteUri); } } } } } // Return the list of strings. return(ret); }
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(); } }
// Get a list of the titles of all the slides in the presentation. public static System.Collections.Generic.IList <string> GetSlideTitles(string presentationFile) { // Open the presentation as read-only. using (DocumentFormat.OpenXml.Packaging.PresentationDocument presentationDocument = DocumentFormat.OpenXml.Packaging.PresentationDocument.Open(presentationFile, false)) { return(GetSlideTitles(presentationDocument)); } }
public static int CountSlides(string presentationFile) { // Open the presentation as read-only. using (DocumentFormat.OpenXml.Packaging.PresentationDocument presentationDocument = DocumentFormat.OpenXml.Packaging.PresentationDocument.Open(presentationFile, false)) { // Pass the presentation to the next CountSlides method // and return the slide count. return(CountSlides(presentationDocument)); } }
// Get all the text in a slide. public static string[] GetAllTextInSlide(string presentationFile, int slideIndex) { // Open the presentation as read-only. using (DocumentFormat.OpenXml.Packaging.PresentationDocument presentationDocument = DocumentFormat.OpenXml.Packaging.PresentationDocument.Open(presentationFile, false)) { // Pass the presentation and the slide index // to the next GetAllTextInSlide method, and // then return the array of strings it returns. return(GetAllTextInSlide(presentationDocument, slideIndex)); } }
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); }