使用 Aspose.Slide 獲取PPT中的所有幻燈片的標題


本文使用的是第三方類庫 Aspose.Slide,如果你使用的是OpenXml可以看下面的鏈接,原理是相同的,這個文章里也有對Xml標簽的詳細解釋。

如何:獲取演示文稿中的所有幻燈片的標題

原理:

  原理說白了很簡單,明白了原理大家都寫得出來。

  簡單說,一個PPT里有多個幻燈片,一個幻燈片里有多個Shape, Shape會有一個Plcaeholder,Placeholder的Type屬性來決定是否是標題。

  Aspose的對像 IPresentation->Slide->Shape->PlaceHolder

 

代碼:

判斷Shape是一個Title,采用了擴展方法的方式:

    public static class ShapeExtension
    {
        public static bool IsTitleShape(this IShape p_shape)
        {
            if (p_shape == null)
            {
                return false;
            }

            var placeholder = p_shape.Placeholder;
            if (placeholder != null)
            {
                switch (placeholder.Type)
                {
                    // Any title shape.
                    case PlaceholderType.Title:
                    // A centered title.
                    case PlaceholderType.CenteredTitle:
                        return true;

                    default:
                        return false;
                }
            }

            return false;
        }
    }
View Code

我們定義一個SlideTitle來存放

    public class SlideTitle
    {
        public int PageNum { get; set; }

        public int TitleCount { get; set; }

        public string[] Titles { get; set; }
    }
View Code

再擴展IPresentation對象,增加一個GetTitles的方法

    public static class PresentationExtension
    {
        public static IEnumerable<SlideTitle> GetTitles(this IPresentation p_presentation)
        {
            var presentation = p_presentation;
            if (presentation != null)
            {
                foreach (var slide in presentation.Slides)
                {
                    List<string> titles = new List<string>();

                    foreach (var shape in slide.Shapes)
                    {
                        if (!shape.IsTitleShape())
                        {
                            continue;
                        }

                        var autoShape = shape as AutoShape;
                        if (autoShape == null)
                        {
                            continue;
                        }

                        titles.Add(autoShape.TextFrame.Text);
                    }

                    var title = new SlideTitle()
                    {
                        PageNum = slide.SlideNumber,
                        TitleCount = titles.Count,
                        Titles = titles.ToArray()
                    };

                    yield return title;
                }
            }
        }
    }

 

總結:

  這東西本身,很簡單的東西,主要就是判斷哪個屬性。幸好查到了微軟的那篇文章。

 

本文原創

轉載請注明出處:http://www.cnblogs.com/gaoshang212/p/4440807.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM