E不小心

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

本文使用的是第三方类库 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

 

posted on 2015-04-20 10:27  E不小心  阅读(3619)  评论(2编辑  收藏  举报