本文使用的是第三方類庫(kù) Aspose.Slide,如果你使用的是Openxml可以看下面的鏈接,原理是相同的,這個(gè)文章里也有對(duì)Xml標(biāo)簽的詳細(xì)解釋。
如何:獲取演示文稿中的所有幻燈片的標(biāo)題
原理:
原理說白了很簡(jiǎn)單,明白了原理大家都寫得出來。
簡(jiǎn)單說,一個(gè)PPT里有多個(gè)幻燈片,一個(gè)幻燈片里有多個(gè)Shape, Shape會(huì)有一個(gè)Plcaeholder,Placeholder的Type屬性來決定是否是標(biāo)題。
Aspose的對(duì)像ipresentation->Slide->Shape->PlaceHolder
代碼:
判斷Shape是一個(gè)Title,采用了擴(kuò)展方法的方式:
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
我們定義一個(gè)SlideTitle來存放
public class SlideTitle { public int PageNum { get; set; } public int TitleCount { get; set; } public string[] Titles { get; set; } }View Code
再擴(kuò)展IPResentation對(duì)象,增加一個(gè)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; } } } }
總結(jié):
這東西本身,很簡(jiǎn)單的東西,主要就是判斷哪個(gè)屬性。幸好查到了微軟的那篇文章。
本文原創(chuàng)
轉(zhuǎn)載請(qǐng)注明出處:http://www.companysz.com/gaoshang212/p/4440807.html
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注