Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        // 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);
        }