Share via


Anleitung: Hinzufügen eines Kommentars zu einer Folie in einer Präsentation

Letzte Änderung: Donnerstag, 14. Oktober 2010

Gilt für: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

Inhalt dieses Artikels
Abrufen eines PresentationDocument-Objekts
Grundlegende Präsentationsdokumentstruktur
Die Struktur des Kommentarelements
Funktionsweise des Beispielcodes
Beispielcode

In diesem Thema wird erklärt, wie Sie mithilfe der Klassen im Open XML SDK 2.0 für Microsoft Office programmgesteuert der ersten Folie in einer Präsentation einen Kommentar hinzufügen können.

Die folgenden Assemblydirektiven sind zum Kompilieren des Codes in diesem Thema erforderlich.

using System;
using System.Linq;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Packaging;
Imports System
Imports System.Linq
Imports DocumentFormat.OpenXml.Presentation
Imports DocumentFormat.OpenXml.Packaging

Abrufen eines PresentationDocument-Objekts

Im Open XML SDK stellt die PresentationDocument-Klasse ein Präsentationsdokumentpaket dar. Zum Arbeiten mit einem Präsentationsdokument müssen Sie zuerst eine Instanz der PresentationDocument-Klasse erstellen und anschließend mit dieser Instanz arbeiten. Rufen Sie zum Erstellen der Klasseninstanz anhand des Dokuments die Open(String, Boolean)-Methode auf, die einen Dateipfad und als zweiten Parameter einen booleschen Wert verwendet, um anzugeben, dass ein Dokument bearbeitbar ist. Zum Öffnen eines Dokuments mit Lese-/Schreibzugriff geben Sie den Wert true für diesen Parameter an (siehe die folgende using-Anweisung). In diesem Code ist der file-Parameter eine Zeichenfolge, die den Pfad der Datei darstellt, in dem Sie das Dokument öffnen möchten.

using (PresentationDocument doc = PresentationDocument.Open(file, true))
{
    // Insert other code here.
}
Using doc As PresentationDocument = PresentationDocument.Open(file, True)
    ' Insert other code here.
End Using

Die using-Anweisung ist eine empfohlene Alternative zur herkömmlichen Reihenfolge ".Open, .Save, .Close". Sie stellt sicher, dass die Dispose-Methode (vom Open XML SDK verwendete interne Methode zum Bereinigen von Ressourcen) bei Erreichen der schließenden Klammer automatisch aufgerufen wird. Der auf die using-Anweisung folgende Block richtet einen Bereich für das Objekt ein, das in der using-Anweisung erstellt oder benannt wird, in diesem Fall doc.

Grundlegende Präsentationsdokumentstruktur

Die grundlegende Dokumentstruktur eines PresentationML-Dokuments besteht aus mehreren Teilen, unter anderem dem Hauptteil, der die Präsentationsdefinition enthält. Im folgenden Text aus der Spezifikation ISO/IEC 29500 wird die Gesamtstruktur eines PresentationML-Pakets eingeführt.

Der Hauptteil eines PresentationML-Pakets beginnt mit dem Stammelement der Präsentation. Dieses Element enthält eine Präsentation, die wiederum auf eine Folienliste, eine Folienmasterliste, eine Notizenmasterliste und eine Handzettelmaster-Liste verweist. Die Folienliste verweist auf alle Folien in der Präsentation. Die Folienmasterliste verweist auf sämtliche in der Präsentation verwendeten Folienmaster. Der Notizenmaster enthält Informationen zur Formatierung der Notizenseiten. Der Handzettelmaster beschreibt das Aussehen eines Handzettels.

Ein Handzettel ist ein gedruckter Foliensatz, der an das Publikum verteilt werden kann.

Neben Text und Grafiken kann jede Folie Kommentare und Notizen enthalten, ein Layout aufweisen und Teil mindestens einer zielgruppenorientierten Präsentation sein. Ein Kommentar ist eine an die Person, die die Präsentationsfolien verwaltet, gerichtete Anmerkung. Eine Notiz ist eine Erinnerung oder eine kurze Textstelle, die für den Präsentator oder das Publikum bestimmt ist.

Andere Features, die ein PresentationML-Dokument enthalten kann, sind Animationen, Audio, Video und Übergänge zwischen den Folien.

Ein PresentationML-Dokument wird nicht als ein großer Textkörper in einem einzelnen Teil gespeichert. Die Elemente, mit deren Hilfe bestimmte Funktionsgruppierungen erfolgen, sind stattdessen in mehreren Teilen gespeichert. Beispielsweise sind alle Kommentare in einem Dokument in einem Kommentarteil gespeichert, wobei jede Folie über einen eigenen Teil verfügt.

© ISO/IEC29500: 2008.

Das folgende XML-Codebeispiel stellt eine Präsentation dar, die zwei Folien mit den IDs 267 und 256 enthält.

<p:presentation xmlns:p="…" … > 
   <p:sldMasterIdLst>
      <p:sldMasterId
         xmlns:rel="http://…/relationships" rel:id="rId1"/>
   </p:sldMasterIdLst>
   <p:notesMasterIdLst>
      <p:notesMasterId
         xmlns:rel="http://…/relationships" rel:id="rId4"/>
   </p:notesMasterIdLst>
   <p:handoutMasterIdLst>
      <p:handoutMasterId
         xmlns:rel="http://…/relationships" rel:id="rId5"/>
   </p:handoutMasterIdLst>
   <p:sldIdLst>
      <p:sldId id="267"
         xmlns:rel="http://…/relationships" rel:id="rId2"/>
      <p:sldId id="256"
         xmlns:rel="http://…/relationships" rel:id="rId3"/>
   </p:sldIdLst>
       <p:sldSz cx="9144000" cy="6858000"/>
   <p:notesSz cx="6858000" cy="9144000"/>
</p:presentation>

Mithilfe des Open XML SDK 2.0 können Sie eine Dokumentstruktur und Inhalte erstellen, indem Sie stark typisierte Klassen verwenden, die PresentationML-Elementen entsprechen. Diese Klassen sind im DocumentFormat.OpenXml.Presentation-Namespace enthalten. Die folgende Tabelle enthält die Namen der Klassen, die den Elementen sld, sldLayout, sldMaster und notesMaster entsprechen.

PresentationML-Element

Open XML SDK 2.0-Klasse

Beschreibung

sld

Slide

Präsentationsfolie. Das SlidePart-Stammelement.

sldLayout

SlideLayout

Das Folienlayout. Das SlideLayoutPart-Stammelement.

sldMaster

SlideMaster

Der Folienmaster. Das SlideMasterPart-Stammelement.

notesMaster

NotesMaster

Notizenmaster (oder Handzettelmaster). Das NotesMasterPart-Stammelement.

Die Struktur des Kommentarelements

Ein Kommentar ist eine an eine Folie angefügte Textnotiz und hat den Hauptzweck, den Lesern der Präsentation die Möglichkeit zu bieten, dem Autor der Präsentation Feedback zu geben. Jeder Kommentar besteht aus einer unformatierten Textzeichenfolge sowie Informationen zum Autor und ist an eine bestimmte Stelle auf einer Folie angefügt. Kommentare können beim Bearbeiten der Präsentation sichtbar sein, werden jedoch beim Halten der Bildschirmpräsentation nicht angezeigt. Die anzeigende Anwendung bestimmt, wann Kommentare angezeigt und wie sie visuell dargestellt werden.

Das folgende XML-Element gibt einen einzelnen an eine Folie angefügten Kommentar an. Es enthält den Text des Kommentars (text), seine Position auf der Folie (pos) und Attribute, die auf den Autor (authorId), Datum und Uhrzeit (dt) und den Kommentarindex (idx) verweisen.

<p:cm authorId="0" dt="2006-08-28T17:26:44.129" idx="1">
    <p:pos x="10" y="10"/>
    <p:text>Add diagram to clarify.</p:text>
</p:cm>

Die folgende Tabelle enthält die Definitionen der Member und Attribute des cm-Kommentarelements

Member/Attribut

Definition

authorId

Verweist auf die ID eines Autors in der Kommentarautorenliste des Dokuments.

dt

Datum und Uhrzeit der letzten Änderung dieses Kommentars.

idx

Eine Kennung dieses Kommentars, die in einer Liste aller Kommentare dieses Autors in diesem Dokument eindeutig ist. Der erste Kommentar eines Autors in einem Dokument hat den Index 1.

pos

Die Positionierungsinformationen zum Platzieren eines Kommentars auf einer Folienoberfläche.

text

Kommentartext.

extLst

Gibt die Erweiterungsliste mit Modifikationsmöglichkeit an, in der alle künftigen Erweiterungen des Elementtyps ext definiert werden. Die Erweiterung wird gemeinsam mit den entsprechenden künftigen Erweiterungen genutzt, um die Speicherfähigkeiten des PresentationML-Frameworks zu vergrößern. Dadurch können verschiedene neue Arten von Daten systeminhärent im Framework gespeichert werden.

Im folgenden XML-Codebeispiel werden die Member des cm-Elements zusätzlich zu den erforderlichen und optionalen Attributen definiert.

<complexType name="CT_Comment">
   <sequence>
       <element name="pos" type="a:CT_Point2D" minOccurs="1" maxOccurs="1"/>
       <element name="text" type="xsd:string" minOccurs="1" maxOccurs="1"/>
       <element name="extLst" type="CT_ExtensionListModify" minOccurs="0" maxOccurs="1"/>
   </sequence>
   <attribute name="authorId" type="xsd:unsignedInt" use="required"/>
   <attribute name="dt" type="xsd:dateTime" use="optional"/>
   <attribute name="idx" type="ST_Index" use="required"/>
</complexType>

Funktionsweise des Beispielcodes

Im Beispielcode wird das in der using-Anweisung angegebene Präsentationsdokument geöffnet. Dann wird der CommentAuthorsPart-Teil instanziiert und überprüft, ob ein Kommentarautorenteil vorhanden ist. Falls nicht, wird einer hinzugefügt.

// Declare a CommentAuthorsPart object.
CommentAuthorsPart authorsPart;

// Verify that there is an existing comment authors part. 
if (doc.PresentationPart.CommentAuthorsPart == null)
{
    // If not, add a new one.
    authorsPart = doc.PresentationPart.AddNewPart<CommentAuthorsPart>();
}
else
{
    authorsPart = doc.PresentationPart.CommentAuthorsPart;
}
' Declare a CommentAuthorsPart object.
Dim authorsPart As CommentAuthorsPart

' Verify that there is an existing comment authors part. 
If doc.PresentationPart.CommentAuthorsPart Is Nothing Then
    ' If not, add a new one.
    authorsPart = doc.PresentationPart.AddNewPart(Of CommentAuthorsPart)()
Else
    authorsPart = doc.PresentationPart.CommentAuthorsPart
End If

Der Code bestimmt, ob der Kommentarautorenteil bereits eine Kommentarautorenliste enthält. Falls nicht, fügt er einen hinzu. Außerdem wird überprüft, ob der übergebene Autor in der Liste der vorhandenen Kommentarautoren enthalten ist. Falls ja, wird die vorhandene Autoren-ID zugewiesen. Falls nein, wird der Liste der Kommentarautoren ein neuer Autor hinzugefügt, dem eine Autoren-ID und die Parameterwerte zugewiesen werden.

// Verify that there is a comment author list in the comment authors part.
if (authorsPart.CommentAuthorList == null)
{
    // If not, add a new one.
    authorsPart.CommentAuthorList = new CommentAuthorList();
}

// Declare a new author ID.
uint authorId = 0;
CommentAuthor author = null;

// If there are existing child elements in the comment authors list...
if (authorsPart.CommentAuthorList.HasChildren)
{
    // Verify that the author passed in is on the list.
    var authors = authorsPart.CommentAuthorList.Elements<CommentAuthor>().Where(a => a.Name == name && a.Initials == initials);

    // If so...
    if (authors.Any())
    {
        // Assign the new comment author the existing author ID.
        author = authors.First();
        authorId = author.Id;
    }

    // If not...
    if (author == null)
    {
        // Assign the author passed in a new ID                        
        authorId = authorsPart.CommentAuthorList.Elements<CommentAuthor>().Select(a => a.Id.Value).Max();
    }
}

// If there are no existing child elements in the comment authors list.
if (author == null)
{
    authorId++;

    // Add a new child element(comment author) to the comment author list.
    author = authorsPart.CommentAuthorList.AppendChild<CommentAuthor>
        (new CommentAuthor()
        {
            Id = authorId,
            Name = name,
            Initials = initials,
            ColorIndex = 0
        });
}
' Verify that there is a comment author list in the comment authors part.
If authorsPart.CommentAuthorList Is Nothing Then
    ' If not, add a new one.
    authorsPart.CommentAuthorList = New CommentAuthorList()
End If

' Declare a new author ID.
Dim authorId As UInteger = 0
Dim author As CommentAuthor = Nothing

' If there are existing child elements in the comment authors list...
If authorsPart.CommentAuthorList.HasChildren Then
    ' Verify that the author passed in is on the list.
    Dim authors = authorsPart.CommentAuthorList.Elements(Of CommentAuthor)().Where(Function(a) a.Name = name AndAlso a.Initials = initials)

    ' If so...
    If authors.Any() Then
        ' Assign the new comment author the existing author ID.
        author = authors.First()
        authorId = author.Id
    End If

    ' If not...
    If author Is Nothing Then
        ' Assign the author passed in a new ID                        
        authorId = authorsPart.CommentAuthorList.Elements(Of CommentAuthor)().Select(Function(a) a.Id.Value).Max()
    End If
End If

' If there are no existing child elements in the comment authors list.
If author Is Nothing Then
    authorId += 1

    ' Add a new child element(comment author) to the comment author list.
    author = authorsPart.CommentAuthorList.AppendChild(Of CommentAuthor) (New CommentAuthor() With {.Id = authorId, .Name = name, .Initials = initials, .ColorIndex = 0})
End If

Im folgenden Codesegment ruft der Code die erste Folie in der Präsentation ab, indem die GetFirstSlide-Methode aufgerufen wird. Anschließend wird überprüft, ob in der Folie ein Kommentarteil vorhanden ist. Falls nicht, wird einer hinzugefügt. Der Code prüft auch, ob im Kommentarteil eine Kommentarliste vorhanden ist. Falls nicht, wird eine erstellt.

// Get the first slide, using the GetFirstSlide method.
SlidePart slidePart1 = GetFirstSlide(doc);

// Declare a comments part.
SlideCommentsPart commentsPart;

// Verify that there is a comments part in the first slide part.
if (slidePart1.GetPartsOfType<SlideCommentsPart>().Count() == 0)
{
    // If not, add a new comments part.
    commentsPart = slidePart1.AddNewPart<SlideCommentsPart>();
}
else
{
    // Else, use the first comments part in the slide part.
    commentsPart = slidePart1.SlideCommentsPart;
}

// If the comment list does not exist.
if (commentsPart.CommentList == null)
{
    // Add a new comments list.
    commentsPart.CommentList = new CommentList();
}
' Get the first slide, using the GetFirstSlide method.
Dim slidePart1 As SlidePart = GetFirstSlide(doc)

' Declare a comments part.
Dim commentsPart As SlideCommentsPart

' Verify that there is a comments part in the first slide part.
If slidePart1.GetPartsOfType(Of SlideCommentsPart)().Count() = 0 Then
    ' If not, add a new comments part.
    commentsPart = slidePart1.AddNewPart(Of SlideCommentsPart)()
Else
    ' Else, use the first comments part in the slide part.
    commentsPart = slidePart1.SlideCommentsPart
End If

' If the comment list does not exist.
If commentsPart.CommentList Is Nothing Then
    ' Add a new comments list.
    commentsPart.CommentList = New CommentList()
End If

Der Code ruft anschließend die ID des neuen Kommentars ab und fügt den angegebenen Kommentar mit dem angegebenen Text an der angegebenen Position ein. Anschließend werden der Kommentarautorenteil und der Kommentarteil gespeichert.

// Get the new comment ID.
uint commentIdx = author.LastIndex == null ? 1 : author.LastIndex + 1;
author.LastIndex = commentIdx;

// Add a new comment.
Comment comment = commentsPart.CommentList.AppendChild<Comment>(
new Comment()
{
    AuthorId = authorId,
    Index = commentIdx,
    DateTime = DateTime.Now
});

// Add the position child node to the comment element.
comment.Append(
new Position() { X = 100, Y = 200 },
new Text() { Text = text }); 

// Save the comment authors part.
authorsPart.CommentAuthorList.Save();

// Save the comments part.
commentsPart.CommentList.Save();
' Get the new comment ID.
Dim commentIdx As UInteger = If(author.LastIndex Is Nothing, 1, author.LastIndex + 1)
author.LastIndex = commentIdx

' Add a new comment.
Dim comment As Comment = commentsPart.CommentList.AppendChild(Of Comment)(New Comment() With {.AuthorId = authorId, .Index = commentIdx, .DateTime = Date.Now})

' Add the position child node to the comment element.
comment.Append(New Position() With {.X = 100, .Y = 200}, New Text() With {.Text = text})

' Save the comment authors part.
authorsPart.CommentAuthorList.Save()

' Save the comments part.
commentsPart.CommentList.Save()

Beispielcode

Die AddCommentToPresentation-Methode dient zum Hinzufügen eines Kommentars zu einer Folie. Sie verwendet als Parameter den Namen und Pfad der Quellpräsentationsdatei, die Initialen und den Namen des Kommentarautors sowie den Text des hinzuzufügenden Kommentars. Sie fügt der Liste der Kommentarautoren einen Autor hinzu und fügt anschließend den angegebenen Kommentartext an den festgelegten Koordinaten auf der ersten Folie der Präsentation ein.

Die zweite Methode, GetFirstSlide, dient zum Abrufen der ersten Folie der Präsentation. Sie verwendet das übergebene PresentationDocument-Objekt, ruft dessen Präsentationsteil ab und ruft anschließend die ID der ersten Folie in der Folienliste ab. Danach ruft sie die Beziehungs-ID der Folie und den Folienteil aus der Beziehungs-ID ab und gibt den Folienteil an die aufrufende Methode zurück.

Das folgende Codebeispiel enthält einen Aufruf der AddCommentToPresentation-Methode, die zum Hinzufügen der angegebenen Kommentarzeichenfolge zur ersten Folie in der Präsentationsdatei Myppt1.pptx dient.

AddCommentToPresentation(@"C:\Users\Public\Documents\Myppt1.pptx", 
"Katie Jordan", "KJ", 
"This is my programmatically added comment.");
AddCommentToPresentation("C:\Users\Public\Documents\Myppt1.pptx", _
"Katie Jordan", "KJ", _
"This is my programmatically added comment.")

Hinweis

Öffnen Sie zum Abrufen des exakten Namens und der Initialen des Autors die Präsentationsdatei, klicken Sie auf den Menüpunkt Datei und dann auf Optionen. Das Fenster PowerPoint-Optionen wird geöffnet, und der Inhalt der Registerkarte Allgemein wird angezeigt. Der Name und die Initialen des Autors müssen den Einträgen für Benutzername und Initialen auf dieser Registerkarte entsprechen.

// Adds a comment to the first slide of the presentation document.
// The presentation document must contain at least one slide.
public static void AddCommentToPresentation(string file, string initials, string name, string text)
{
    using (PresentationDocument doc = PresentationDocument.Open(file, true))
    {

        // Declare a CommentAuthorsPart object.
        CommentAuthorsPart authorsPart;

        // Verify that there is an existing comment authors part. 
        if (doc.PresentationPart.CommentAuthorsPart == null)
        {
            // If not, add a new one.
            authorsPart = doc.PresentationPart.AddNewPart<CommentAuthorsPart>();
        }
        else
        {
            authorsPart = doc.PresentationPart.CommentAuthorsPart;
        }

        // Verify that there is a comment author list in the comment authors part.
        if (authorsPart.CommentAuthorList == null)
        {
            // If not, add a new one.
            authorsPart.CommentAuthorList = new CommentAuthorList();
        }

        // Declare a new author ID.
        uint authorId = 0;
        CommentAuthor author = null;

        // If there are existing child elements in the comment authors list...
        if (authorsPart.CommentAuthorList.HasChildren)
        {
            // Verify that the author passed in is on the list.
            var authors = authorsPart.CommentAuthorList.Elements<CommentAuthor>().Where(a => a.Name == name && a.Initials == initials);

            // If so...
            if (authors.Any())
            {
                // Assign the new comment author the existing author ID.
                author = authors.First();
                authorId = author.Id;
            }

            // If not...
            if (author == null)
            {
                // Assign the author passed in a new ID                        
                authorId = authorsPart.CommentAuthorList.Elements<CommentAuthor>().Select(a => a.Id.Value).Max();
            }
        }

        // If there are no existing child elements in the comment authors list.
        if (author == null)
        {

            authorId++;

            // Add a new child element(comment author) to the comment author list.
            author = authorsPart.CommentAuthorList.AppendChild<CommentAuthor>
                (new CommentAuthor()
                {
                    Id = authorId,
                    Name = name,
                    Initials = initials,
                    ColorIndex = 0
                });
        }

        // Get the first slide, using the GetFirstSlide method.
        SlidePart slidePart1 = GetFirstSlide(doc);

        // Declare a comments part.
        SlideCommentsPart commentsPart;

        // Verify that there is a comments part in the first slide part.
        if (slidePart1.GetPartsOfType<SlideCommentsPart>().Count() == 0)
        {
            // If not, add a new comments part.
            commentsPart = slidePart1.AddNewPart<SlideCommentsPart>();
        }
        else
        {
            // Else, use the first comments part in the slide part.
            commentsPart = slidePart1.GetPartsOfType<SlideCommentsPart>().First();
        }

        // If the comment list does not exist.
        if (commentsPart.CommentList == null)
        {
            // Add a new comments list.
            commentsPart.CommentList = new CommentList();
        }

        // Get the new comment ID.
        uint commentIdx = author.LastIndex == null ? 1 : author.LastIndex + 1;
        author.LastIndex = commentIdx;

        // Add a new comment.
        Comment comment = commentsPart.CommentList.AppendChild<Comment>(
            new Comment()
            {
                AuthorId = authorId,
                Index = commentIdx,
                DateTime = DateTime.Now
            });

        // Add the position child node to the comment element.
        comment.Append(
            new Position() { X = 100, Y = 200 },
            new Text() { Text = text });

        // Save the comment authors part.
        authorsPart.CommentAuthorList.Save();

        // Save the comments part.
        commentsPart.CommentList.Save();
    }
 }
// Get the slide part of the first slide in the presentation document.
public static SlidePart GetFirstSlide(PresentationDocument presentationDocument)
{
    // Get relationship ID of the first slide
    PresentationPart part = presentationDocument.PresentationPart;
    SlideId slideId = part.Presentation.SlideIdList.GetFirstChild<SlideId>();
    string relId = slideId.RelationshipId;

    // Get the slide part by the relationship ID.
    SlidePart slidePart = (SlidePart)part.GetPartById(relId);

    return slidePart;
}
' Adds a comment to the first slide of the presentation document.
' The presentation document must contain at least one slide.
Public Sub AddCommentToPresentation(ByVal file As String, _
            ByVal initials As String, _
            ByVal name As String, _
            ByVal text As String)

   Dim doc As PresentationDocument = _
      PresentationDocument.Open(file, True)

   Using (doc)

      ' Declare a CommentAuthorsPart object.
      Dim authorsPart As CommentAuthorsPart

      ' Verify that there is an existing comment authors part.
      If (doc.PresentationPart.CommentAuthorsPart Is Nothing) Then

         ' If not, add a new one.
         authorsPart = doc.PresentationPart.AddNewPart(Of CommentAuthorsPart)()
      Else
         authorsPart = doc.PresentationPart.CommentAuthorsPart
      End If

      ' Verify that there is a comment author list in the comment authors part.
      If (authorsPart.CommentAuthorList Is Nothing) Then

         ' If not, add a new one.
         authorsPart.CommentAuthorList = New CommentAuthorList()
      End If

      ' Declare a new author ID.
      Dim authorId As UInteger = 0
      Dim author As CommentAuthor = Nothing

      ' If there are existing child elements in the comment authors list.
      If authorsPart.CommentAuthorList.HasChildren = True Then

         ' Verify that the author passed in is on the list.
         Dim authors = authorsPart.CommentAuthorList.Elements(Of CommentAuthor)().Where _
          (Function(a) a.Name = name AndAlso a.Initials = initials)

         ' If so...
         If (authors.Any()) Then

            ' Assign the new comment author the existing ID.
            author = authors.First()
            authorId = author.Id
         End If

         ' If not...
         If (author Is Nothing) Then

            ' Assign the author passed in a new ID.
            authorId = _
            authorsPart.CommentAuthorList.Elements(Of CommentAuthor)().Select(Function(a) a.Id.Value).Max()
         End If

      End If

      ' If there are no existing child elements in the comment authors list.
      If (author Is Nothing) Then

         authorId = authorId + 1

         ' Add a new child element (comment author) to the comment author list.
         author = (authorsPart.CommentAuthorList.AppendChild(Of CommentAuthor) _
          (New CommentAuthor() With {.Id = authorId, _
           .Name = name, _
           .Initials = initials, _
           .ColorIndex = 0}))
      End If

      ' Get the first slide, using the GetFirstSlide() method.
      Dim slidePart1 As SlidePart
      slidePart1 = GetFirstSlide(doc)

      ' Declare a comments part.
      Dim commentsPart As SlideCommentsPart

      ' Verify that there is a comments part in the first slide part.
      If slidePart1.GetPartsOfType(Of SlideCommentsPart)().Count() = 0 Then

         ' If not, add a new comments part.
         commentsPart = slidePart1.AddNewPart(Of SlideCommentsPart)()
      Else

         ' Else, use the first comments part in the slide part.
         commentsPart = _
          slidePart1.GetPartsOfType(Of SlideCommentsPart)().First()
      End If

      ' If the comment list does not exist.
      If (commentsPart.CommentList Is Nothing) Then

         ' Add a new comments list.
         commentsPart.CommentList = New CommentList()
      End If

      ' Get the new comment ID.
      Dim commentIdx As UInteger
      If author.LastIndex Is Nothing Then
         commentIdx = 1
      Else
         commentIdx = CType(author.LastIndex, UInteger) + 1
      End If

      author.LastIndex = commentIdx

      ' Add a new comment.
      Dim comment As Comment = _
       (commentsPart.CommentList.AppendChild(Of Comment)(New Comment() _
         With {.AuthorId = authorId, .Index = commentIdx, .DateTime = DateTime.Now}))

      ' Add the position child node to the comment element.
      comment.Append(New Position() With _
           {.X = 100, .Y = 200}, New Text() With {.Text = text})


      ' Save comment authors part.
      authorsPart.CommentAuthorList.Save()

      ' Save comments part.
      commentsPart.CommentList.Save()

   End Using

End Sub

' Get the slide part of the first slide in the presentation document.
Public Function GetFirstSlide(ByVal presentationDocument As PresentationDocument) As SlidePart
   ' Get relationship ID of the first slide
   Dim part As PresentationPart = presentationDocument.PresentationPart
   Dim slideId As SlideId = part.Presentation.SlideIdList.GetFirstChild(Of SlideId)()
   Dim relId As String = slideId.RelationshipId

   ' Get the slide part by the relationship ID.
   Dim slidePart As SlidePart = DirectCast(part.GetPartById(relId), SlidePart)

   Return slidePart
End Function
End Module

Siehe auch

Referenz

Class Library Reference