使用笔记幻灯片

本主题讨论 Open XML SDK for Office NotesSlide 类,以及它与 Open XML 文件格式 PresentationML 架构的关系。


PresentationML 中的备注幻灯片

ISO/IEC 29500(该链接可能指向英文页面) 规范介绍了 Open XML PresentationML <notes/> 元素,此元素用于表示 PresentationML 文档中的备注幻灯片,如下所示:

此元素指定存在备注幻灯片及其相应数据。 备注幻灯片包含所有通用幻灯片元素以及特定于 notes 元素的其他属性。

示例:请考虑以下 PresentationML 备注幻灯片:

<p:notes>
    <p:cSld>
        …
    </p:cSld>
    …
</p:notes>

在上面的示例中,notes 元素指定存在备注幻灯片以及它的所有部件。 请注意 cSld 元素,此元素指定可以显示在任何幻灯片类型中的通用元素,接下来的任何元素都用于指定此备注幻灯片的其他非通用属性。

© ISO/IEC 29500:2016

<notes/> 元素是 PresentationML 备注幻灯片部件的根元素。 有关构成 PresentationML 文档的部件和元素的整体结构的详细信息,请参阅 PresentationML 文档的结构

下表列出了处理备注幻灯片时使用的 元素的 <notes/> 子元素以及与之对应的 Open XML SDK 类。

PresentationML 元素 Open XML SDK 类
<clrMapOvr/> ColorMapOverride
<cSld/> CommonSlideData
<extLst/> ExtensionListWithModification

ISO/IEC 29500(该链接可能指向英文页面) 规范中的以下表介绍了 <notes/> 元素。

属性 说明
showMasterPhAnim (显示母版占位符动画) 指定是否显示母版幻灯片中占位符的动画。

此属性的可能值由 W3C XML 架构 boolean 数据类型定义。
showMasterSp (显示母版形状) 指定是否在幻灯片上显示母版幻灯片上的形状。

此属性的可能值由 W3C XML 架构 boolean 数据类型定义。

© ISO/IEC 29500:2016


Open XML SDK NotesSlide 类

OXML SDK NotesSlide 类表示 <notes/> 在 PresentationML 文档的 Open XML 文件格式架构中定义的元素。 NotesSlide使用 类可操作 PresentationML 文档中的各个<notes/>元素。

以下列表中显示了表示 元素的 <notes/> 子元素并且因此通常与 NotesSlide 类关联的类。

  • ColorMapOverride 类

    ColorMapOverride 对应于 <clrMapOvr/> 元素。 ISO/IEC 29500 规范中的以下信息介绍了 元素<clrMapOvr/>

    此元素提供一种机制,使用此机制可以替代 <ClrMap/> 元素中列出的配色方案。 如果存在 <masterClrMapping/> 子元素,则使用母版定义的配色方案。 如果存在 <overrideClrMapping/> 子元素,则它定义特定于父备注幻灯片、演示文稿幻灯片或幻灯片版式的新配色方案。

© ISO/IEC 29500:2016

  • CommonSlideData 类

    CommonSlideData 对应于 <cSld/> 元素。 ISO/IEC 29500 规范中的以下信息介绍了 元素<cSld/>

    此元素指定与所有幻灯片类型相关的幻灯片信息类型的容器。 所有幻灯片都共享一组独立于幻灯片类型的通用属性;任何特定幻灯片的这些属性的描述都存储在该幻灯片的 <cSld/> 容器中。 由父元素指明的特定于幻灯片类型的幻灯片数据存储在其他位置。

    <cSld/> 中的实际数据只描述特定的父幻灯片;这些数据只是存储的对所有幻灯片通用的信息类型。

    © ISO/IEC 29500:2016

  • ExtensionListWithModification 类

    ExtensionListWithModification 对应于 <extLst/>元素。 ISO/IEC 29500 规范中的以下信息介绍了 元素<extLst/>

    此元素指定具有修改能力的扩展列表,在该列表中定义元素类型 <ext/> 的所有未来扩展。 扩展列表以及相应的未来扩展用于扩展 PresentationML 框架的存储功能。 这允许在框架中本机存储各种新类型的数据。

    [注意:使用此 extLst 元素,生成应用程序可以存储有关此扩展属性是否已修改的相关信息。 注释结束]

    © ISO/IEC 29500:2016


处理 NotesSlide 类

如下面的 Open XML SDK 代码示例所示,类的每个实例 NotesSlide 都与 类的 NotesSlidePart 一个实例相关联,该实例表示备注幻灯片部件、PresentationML 演示文稿文件包的一部分以及演示文稿文件中每个备注幻灯片所需的部件。 每个 NotesSlide 类实例还可以与 类的 NotesMaster 一个实例相关联,该实例又与类表示 NotesMasterPart 的类似名称的演示部件相关联。

NotesSlide因此,表示 元素的<notes/>类也与一系列表示元素子元素<notes/>的其他类相关联。 如以下代码示例所示,这些类包括 CommonSlideData 类和 类 ColorMapOverride 。 类 ShapeTreeShape 类又与 CommonSlideData 类相关联。


Open XML SDK 代码示例

在以下代码片段中,表示 P 命名空间,表示 D 命名空间。

在下面的代码片段中,使用 打开 Presentation.Open 演示文稿,如果演示文稿还没有 ,则检索或添加第 SlidePart 一个 SlidePart

using (PresentationDocument presentationDocument = PresentationDocument.Open(pptxPath, true) ?? throw new Exception("Presentation Document does not exist"))
{
    // Get the first slide in the presentation or use the InsertNewSlide.InsertNewSlideIntoPresentation helper method to insert a new slide.
    SlidePart slidePart = presentationDocument.PresentationPart?.SlideParts.FirstOrDefault() ?? InsertNewSlideNS.InsertNewSlide(presentationDocument, 1, "my new slide");

    // Add a new NoteSlidePart if one does not already exist
    NotesSlidePart notesSlidePart = slidePart.NotesSlidePart ?? slidePart.AddNewPart<NotesSlidePart>();

在此代码片段中, NoteSlide 如果尚不存在, 则会将 NoteSlidePart 添加到 。 类 NotesSlide 构造函数创建 类的 CommonSlideData 实例。 CommonSlideData类构造函数创建 类的ShapeTree实例,其构造函数又创建其他类实例:类的NonVisualGroupShapeProperties实例、类的GroupShapeProperties实例和 类的Shape实例。

// Add a NoteSlide to the NoteSlidePart if one does not already exist.
notesSlidePart.NotesSlide ??= new P.NotesSlide(
    new P.CommonSlideData(
        new P.ShapeTree(
            new P.NonVisualGroupShapeProperties(
                new P.NonVisualDrawingProperties() { Id = 1, Name = "" },
                new P.NonVisualGroupShapeDrawingProperties(),
                new P.ApplicationNonVisualDrawingProperties()),
            new P.GroupShapeProperties(
                new D.Transform2D(
                    new D.Offset() { X = 0, Y = 0 },
                    new D.Extents() { Cx = 0, Cy = 0 },
                    new D.ChildOffset() { X = 0, Y = 0 },
                    new D.ChildExtents() { Cx = 0, Cy = 0 })),

构造Shape函数创建 、 ShapePropertiesTextBody 类的NonVisualShapeProperties实例及其所需的子元素。 包含 TextBody 具有 Paragraph的 , Run其中包含注释的文本。 然后将幻灯片部件添加到备注幻灯片部件。

            new P.Shape(
                new P.NonVisualShapeProperties(
                    new P.NonVisualDrawingProperties() { Id = 3, Name = "test Placeholder 3" },
                    new P.NonVisualShapeDrawingProperties(),
                    new P.ApplicationNonVisualDrawingProperties(
                        new P.PlaceholderShape() { Type = PlaceholderValues.Body, Index = 1 })),
                new P.ShapeProperties(),
                new P.TextBody(
                    new D.BodyProperties(),
                    new D.Paragraph(
                        new D.Run(
                            new D.Text("This is a test note!"))))))));

notesSlidePart.AddPart(slidePart);

使用上述代码创建的备注幻灯片部件包含以下 XML

<?xml version="1.0" encoding="utf-8"?>
<p:notes xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
  <p:cSld>
    <p:spTree>
      <p:nvGrpSpPr>
        <p:cNvPr id="1" name=""/>
        <p:cNvGrpSpPr/>
        <p:nvPr/>
      </p:nvGrpSpPr>
      <p:grpSpPr>
        <a:xfrm xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
          <a:off x="0" y="0"/>
          <a:ext cx="0" cy="0"/>
          <a:chOff x="0" y="0"/>
          <a:chExt cx="0" cy="0"/>
        </a:xfrm>
      </p:grpSpPr>
      <p:sp>
        <p:nvSpPr>
          <p:cNvPr id="3" name="test Placeholder 3"/>
          <p:cNvSpPr/>
          <p:nvPr/>
        </p:nvSpPr>
        <p:spPr/>
        <p:txBody>
          <a:bodyPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>
          <a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
            <a:r>
              <a:t>This is a test note!</a:t>
            </a:r>
          </a:p>
        </p:txBody>
      </p:sp>
    </p:spTree>
  </p:cSld>
</p:notes>

以下代码片段在缺少时添加所需的 NotesMasterPartThemePart

// Add the required NotesMasterPart if it is missing
NotesMasterPart notesMasterPart = notesSlidePart.NotesMasterPart ?? notesSlidePart.AddNewPart<NotesMasterPart>();

// Add a NotesMaster to the NotesMasterPart if not present
notesMasterPart.NotesMaster ??= new NotesMaster(
new P.CommonSlideData(
    new P.ShapeTree(
        new P.NonVisualGroupShapeProperties(
            new P.NonVisualDrawingProperties() { Id = 1, Name = "New Placeholder" },
            new P.NonVisualGroupShapeDrawingProperties(),
            new P.ApplicationNonVisualDrawingProperties()),
        new P.GroupShapeProperties())),
new P.ColorMap()
{
    Background1 = D.ColorSchemeIndexValues.Light1,
    Background2 = D.ColorSchemeIndexValues.Light2,
    Text1 = D.ColorSchemeIndexValues.Dark1,
    Text2 = D.ColorSchemeIndexValues.Dark2,
    Accent1 = D.ColorSchemeIndexValues.Accent1,
    Accent2 = D.ColorSchemeIndexValues.Accent2,
    Accent3 = D.ColorSchemeIndexValues.Accent3,
    Accent4 = D.ColorSchemeIndexValues.Accent4,
    Accent5 = D.ColorSchemeIndexValues.Accent5,
    Accent6 = D.ColorSchemeIndexValues.Accent6,
    Hyperlink = D.ColorSchemeIndexValues.Hyperlink,
    FollowedHyperlink = D.ColorSchemeIndexValues.FollowedHyperlink,
});

// Add a new ThemePart for the NotesMasterPart
ThemePart themePart = notesMasterPart.ThemePart ?? notesMasterPart.AddNewPart<ThemePart>();

// Add the Theme if it is missing
themePart.Theme ??= new Theme(
    new ThemeElements(
        new ColorScheme(
            new Dark1Color(
                new SystemColor() { Val = SystemColorValues.WindowText }),
            new Light1Color(
                new SystemColor() { Val = SystemColorValues.Window }),
            new Dark2Color(
                new RgbColorModelHex() { Val = "f1d7be" }),
            new Light2Color(
                new RgbColorModelHex() { Val = "171717" }),
            new Accent1Color(
                new RgbColorModelHex() { Val = "ea9f7d" }),
            new Accent2Color(
                new RgbColorModelHex() { Val = "168ecd" }),
            new Accent3Color(
                new RgbColorModelHex() { Val = "e694db" }),
            new Accent4Color(
                new RgbColorModelHex() { Val = "f0612a" }),
            new Accent5Color(
                new RgbColorModelHex() { Val = "5fd46c" }),
            new Accent6Color(
                new RgbColorModelHex() { Val = "b158d1" }),
            new D.Hyperlink(
                new RgbColorModelHex() { Val = "699f82" }),
            new FollowedHyperlinkColor(
                new RgbColorModelHex() { Val = "699f82" }))
        { Name = "Office2" },
        new D.FontScheme(
            new MajorFont(
                new LatinFont(),
                new EastAsianFont(),
                new ComplexScriptFont()),
            new MinorFont(
                new LatinFont(),
                new EastAsianFont(),
                new ComplexScriptFont()))
        { Name = "Office2" },
        new FormatScheme(
            new FillStyleList(
                new NoFill(),
                new SolidFill(),
                new D.GradientFill(),
                new D.BlipFill(),
                new D.PatternFill(),
                new GroupFill()),
            new LineStyleList(
                new D.Outline(),
                new D.Outline(),
                new D.Outline()),
            new EffectStyleList(
                new EffectStyle(
                    new EffectList()),
                new EffectStyle(
                    new EffectList()),
                new EffectStyle(
                    new EffectList())),
            new BackgroundFillStyleList(
                new NoFill(),
                new SolidFill(),
                new D.GradientFill(),
                new D.BlipFill(),
                new D.PatternFill(),
                new GroupFill()))
        { Name = "Office2" }),
    new ObjectDefaults(),
    new ExtraColorSchemeList());

示例代码

以下是使用 C# 和 Visual Basic 编写的完整示例代码。

using (PresentationDocument presentationDocument = PresentationDocument.Open(pptxPath, true) ?? throw new Exception("Presentation Document does not exist"))
{
    // Get the first slide in the presentation or use the InsertNewSlide.InsertNewSlideIntoPresentation helper method to insert a new slide.
    SlidePart slidePart = presentationDocument.PresentationPart?.SlideParts.FirstOrDefault() ?? InsertNewSlideNS.InsertNewSlide(presentationDocument, 1, "my new slide");

    // Add a new NoteSlidePart if one does not already exist
    NotesSlidePart notesSlidePart = slidePart.NotesSlidePart ?? slidePart.AddNewPart<NotesSlidePart>();

    // Add a NoteSlide to the NoteSlidePart if one does not already exist.
    notesSlidePart.NotesSlide ??= new P.NotesSlide(
        new P.CommonSlideData(
            new P.ShapeTree(
                new P.NonVisualGroupShapeProperties(
                    new P.NonVisualDrawingProperties() { Id = 1, Name = "" },
                    new P.NonVisualGroupShapeDrawingProperties(),
                    new P.ApplicationNonVisualDrawingProperties()),
                new P.GroupShapeProperties(
                    new D.Transform2D(
                        new D.Offset() { X = 0, Y = 0 },
                        new D.Extents() { Cx = 0, Cy = 0 },
                        new D.ChildOffset() { X = 0, Y = 0 },
                        new D.ChildExtents() { Cx = 0, Cy = 0 })),
                new P.Shape(
                    new P.NonVisualShapeProperties(
                        new P.NonVisualDrawingProperties() { Id = 3, Name = "test Placeholder 3" },
                        new P.NonVisualShapeDrawingProperties(),
                        new P.ApplicationNonVisualDrawingProperties(
                            new P.PlaceholderShape() { Type = PlaceholderValues.Body, Index = 1 })),
                    new P.ShapeProperties(),
                    new P.TextBody(
                        new D.BodyProperties(),
                        new D.Paragraph(
                            new D.Run(
                                new D.Text("This is a test note!"))))))));

    notesSlidePart.AddPart(slidePart);

    // Add the required NotesMasterPart if it is missing
    NotesMasterPart notesMasterPart = notesSlidePart.NotesMasterPart ?? notesSlidePart.AddNewPart<NotesMasterPart>();

    // Add a NotesMaster to the NotesMasterPart if not present
    notesMasterPart.NotesMaster ??= new NotesMaster(
    new P.CommonSlideData(
        new P.ShapeTree(
            new P.NonVisualGroupShapeProperties(
                new P.NonVisualDrawingProperties() { Id = 1, Name = "New Placeholder" },
                new P.NonVisualGroupShapeDrawingProperties(),
                new P.ApplicationNonVisualDrawingProperties()),
            new P.GroupShapeProperties())),
    new P.ColorMap()
    {
        Background1 = D.ColorSchemeIndexValues.Light1,
        Background2 = D.ColorSchemeIndexValues.Light2,
        Text1 = D.ColorSchemeIndexValues.Dark1,
        Text2 = D.ColorSchemeIndexValues.Dark2,
        Accent1 = D.ColorSchemeIndexValues.Accent1,
        Accent2 = D.ColorSchemeIndexValues.Accent2,
        Accent3 = D.ColorSchemeIndexValues.Accent3,
        Accent4 = D.ColorSchemeIndexValues.Accent4,
        Accent5 = D.ColorSchemeIndexValues.Accent5,
        Accent6 = D.ColorSchemeIndexValues.Accent6,
        Hyperlink = D.ColorSchemeIndexValues.Hyperlink,
        FollowedHyperlink = D.ColorSchemeIndexValues.FollowedHyperlink,
    });

    // Add a new ThemePart for the NotesMasterPart
    ThemePart themePart = notesMasterPart.ThemePart ?? notesMasterPart.AddNewPart<ThemePart>();

    // Add the Theme if it is missing
    themePart.Theme ??= new Theme(
        new ThemeElements(
            new ColorScheme(
                new Dark1Color(
                    new SystemColor() { Val = SystemColorValues.WindowText }),
                new Light1Color(
                    new SystemColor() { Val = SystemColorValues.Window }),
                new Dark2Color(
                    new RgbColorModelHex() { Val = "f1d7be" }),
                new Light2Color(
                    new RgbColorModelHex() { Val = "171717" }),
                new Accent1Color(
                    new RgbColorModelHex() { Val = "ea9f7d" }),
                new Accent2Color(
                    new RgbColorModelHex() { Val = "168ecd" }),
                new Accent3Color(
                    new RgbColorModelHex() { Val = "e694db" }),
                new Accent4Color(
                    new RgbColorModelHex() { Val = "f0612a" }),
                new Accent5Color(
                    new RgbColorModelHex() { Val = "5fd46c" }),
                new Accent6Color(
                    new RgbColorModelHex() { Val = "b158d1" }),
                new D.Hyperlink(
                    new RgbColorModelHex() { Val = "699f82" }),
                new FollowedHyperlinkColor(
                    new RgbColorModelHex() { Val = "699f82" }))
            { Name = "Office2" },
            new D.FontScheme(
                new MajorFont(
                    new LatinFont(),
                    new EastAsianFont(),
                    new ComplexScriptFont()),
                new MinorFont(
                    new LatinFont(),
                    new EastAsianFont(),
                    new ComplexScriptFont()))
            { Name = "Office2" },
            new FormatScheme(
                new FillStyleList(
                    new NoFill(),
                    new SolidFill(),
                    new D.GradientFill(),
                    new D.BlipFill(),
                    new D.PatternFill(),
                    new GroupFill()),
                new LineStyleList(
                    new D.Outline(),
                    new D.Outline(),
                    new D.Outline()),
                new EffectStyleList(
                    new EffectStyle(
                        new EffectList()),
                    new EffectStyle(
                        new EffectList()),
                    new EffectStyle(
                        new EffectList())),
                new BackgroundFillStyleList(
                    new NoFill(),
                    new SolidFill(),
                    new D.GradientFill(),
                    new D.BlipFill(),
                    new D.PatternFill(),
                    new GroupFill()))
            { Name = "Office2" }),
        new ObjectDefaults(),
        new ExtraColorSchemeList());
}

另请参阅

关于 Open XML SDK for Office

如何:通过提供文件名创建演示文稿

如何:将新幻灯片插入到演示文稿

如何:从演示文稿中删除幻灯片

如何:将主题应用于演示文稿