如何:在演示文稿文档中检索幻灯片的数量
本主题演示如何使用 Open XML SDK 2.0 for Microsoft Office 中的类以编程方式检索演示文稿文档中幻灯片的数量,不管是否包括隐藏幻灯片,而无需将文档加载到 Microsoft PowerPoint 中。本主题包含一个演示此任务的示例 RetrieveNumberOfSlides 方法。
上次修改时间: 2011年11月29日
适用范围: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010
本文内容
RetrieveNumberOfSlides 方法
调用 RetrieveNumberOfSlides 方法
代码的工作方式
检索所有幻灯片的计数
检索可见幻灯片的计数
示例代码
必须安装 Open XML SDK 2.0(该链接可能指向英文页面) 才能使用本主题中的示例代码。必须在项目中明确引用以下程序集:
WindowsBase
DocumentFormat.OpenXml(由 Open XML SDK 安装)
还必须使用以下 using 指令或 Imports 语句编译本主题中的代码。
using System;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
Imports DocumentFormat.OpenXml.Packaging
RetrieveNumberOfSlides 方法
您可以使用 RetrieveNumberOfSlides 方法获取演示文稿文档中幻灯片的数量,可以选择包括隐藏幻灯片。RetrieveNumberOfSlides 方法接受两个参数:指示要分析的文件路径的字符串和指示是否在计数中包括隐藏幻灯片的布尔值。
public static int RetrieveNumberOfSlides(string fileName,
bool includeHidden = true)
Public Function RetrieveNumberOfSlides(ByVal fileName As String,
Optional ByVal includeHidden As Boolean = True) As Integer
调用 RetrieveNumberOfSlides 方法
该方法返回指示幻灯片数量的整数,可以统计所有幻灯片,也可以仅统计可见幻灯片,具体取决于第二个参数值。要调用该方法,请传递所有参数值,如下面的代码所示。
// Retrieve the number of slides, excluding the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH, false));
// Retrieve the number of slides, including the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH));
' Retrieve the number of slides, excluding the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH, False))
' Retrieve the number of slides, including the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH))
代码的工作方式
代码首先创建一个整数变量 slidesCount,用于存放幻灯片的数量。然后代码使用 PresentationDocument.Open 方法打开指定的演示文稿,并指示该文档应打开供只读访问(最后的 false 参数值)。打开演示文稿后,代码使用 PresentationPart 属性导航到主演示文稿部件,将引用存储在名为 presentationPart 的变量中。
using (PresentationDocument doc =
PresentationDocument.Open(fileName, false))
{
// Get the presentation part of the document.
PresentationPart presentationPart = doc.PresentationPart;
// Code removed here…
}
Return slidesCount;
Using doc As PresentationDocument =
PresentationDocument.Open(fileName, False)
' Get the presentation part of the document.
Dim presentationPart As PresentationPart = doc.PresentationPart
' Code removed here…
End Using
Return slidesCount
检索所有幻灯片的计数
如果演示文稿部件引用不为空(对于任何正确加载到 PowerPoint 中的有效演示文稿,该引用都不应为空),代码随即对演示文稿部件的 SlideParts 属性值调用 Count 方法。如果您请求的是包括隐藏幻灯片在内的所有幻灯片,那么这就是全部的代码。如果您想要排除隐藏幻灯片,则还需要执行一些额外的操作,如下面的代码所示。
if (includeHidden)
{
slidesCount = presentationPart.SlideParts.Count();
}
else
{
// Code removed here…
}
If includeHidden Then
slidesCount = presentationPart.SlideParts.Count()
Else
' Code removed here…
End If
检索可见幻灯片的计数
如果您请求代码应将返回值限制为仅包括可见幻灯片,那么代码必须筛选其幻灯片集合以便仅包括那些其 Show 属性包含值并且该值为 true 的幻灯片。如果 Show 属性为空,也指示该幻灯片可见。一般来说,除非要隐藏幻灯片,否则 PowerPoint 不设置该属性的值。这时最有可能出现这种情况。让 Show 属性存在并具有 true 值的唯一方法就是先隐藏幻灯片再取消隐藏幻灯片。下面的代码使用带 lambda 表达式的 Where 函数执行此工作。
var slides = presentationPart.SlideParts.Where(
(s) => (s.Slide != null) &&
((s.Slide.Show == null) || (s.Slide.Show.HasValue &&
s.Slide.Show.Value)));
slidesCount = slides.Count();
Dim slides = presentationPart.SlideParts.
Where(Function(s) (s.Slide IsNot Nothing) AndAlso
((s.Slide.Show Is Nothing) OrElse
(s.Slide.Show.HasValue AndAlso
s.Slide.Show.Value)))
slidesCount = slides.Count()
示例代码
下面是 C# 和 Visual Basic 中的完整 RetrieveNumberOfSlides 示例代码。
public static int RetrieveNumberOfSlides(string fileName,
bool includeHidden = true)
{
int slidesCount = 0;
using (PresentationDocument doc =
PresentationDocument.Open(fileName, false))
{
// Get the presentation part of the document.
PresentationPart presentationPart = doc.PresentationPart;
if (presentationPart != null)
{
if (includeHidden)
{
slidesCount = presentationPart.SlideParts.Count();
}
else
{
// Each slide can include a Show property, which if hidden
// will contain the value "0". The Show property may not
// exist, and most likely will not, for non-hidden slides.
var slides = presentationPart.SlideParts.Where(
(s) => (s.Slide != null) &&
((s.Slide.Show == null) || (s.Slide.Show.HasValue &&
s.Slide.Show.Value)));
slidesCount = slides.Count();
}
}
}
return slidesCount;
}
Public Function RetrieveNumberOfSlides(ByVal fileName As String,
Optional ByVal includeHidden As Boolean = True) As Integer
Dim slidesCount As Integer = 0
Using doc As PresentationDocument =
PresentationDocument.Open(fileName, False)
' Get the presentation part of the document.
Dim presentationPart As PresentationPart = doc.PresentationPart
If presentationPart IsNot Nothing Then
If includeHidden Then
slidesCount = presentationPart.SlideParts.Count()
Else
' Each slide can include a Show property, which if
' hidden will contain the value "0". The Show property may
' not exist, and most likely will not, for non-hidden slides.
Dim slides = presentationPart.SlideParts.
Where(Function(s) (s.Slide IsNot Nothing) AndAlso
((s.Slide.Show Is Nothing) OrElse
(s.Slide.Show.HasValue AndAlso
s.Slide.Show.Value)))
slidesCount = slides.Count()
End If
End If
End Using
Return slidesCount
End Function
请参阅
引用
其他资源
Retrieving the Number of Slides from PowerPoint 2010 Presentations by Using the Open XML SDK 2.0