程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 使用 Aspose.Slide 獲取PPT中的所有幻燈片的標題,

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

編輯:C#入門知識

使用 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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved