Share via


Anleitung: Ändern der Füllfarbe einer Form 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 Presentation-Objekts
Die Struktur der Formstruktur
Funktionsweise des Beispielcodes
Beispielcode

In diesem Thema wird erklärt, wie Sie die Klassen im Open XML SDK 2.0 für Microsoft Office zum programmgesteuerten Ändern der Füllfarbe einer Form auf der ersten Folie einer Präsentation verwenden.

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

using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using Drawing = DocumentFormat.OpenXml.Drawing;
Imports DocumentFormat.OpenXml.Presentation
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml
Imports Drawing = DocumentFormat.OpenXml.Drawing

Abrufen eines Presentation-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-Methode auf, die einen Dateipfad und als zweiten Parameter einen booleschen Wert verwendet, um anzugeben, ob ein Dokument bearbeitet werden kann. 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 Dateiparameter eine Zeichenfolge, die den Pfad der Datei darstellt, aus der Sie das Dokument öffnen möchten.

using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
    // Insert other code here.
}
Using ppt As PresentationDocument = PresentationDocument.Open(docName, 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 ppt.

Die Struktur der Formstruktur

Die Grunddokumentstruktur eines PresentationML-Dokuments besteht aus zahlreichen Teilen, darunter das Formstrukturelement (sp Tree).

In dem folgenden Text aus der ISO/IEC 29500-Spezifikation wird das Gesamtformat eines PresentationML-Pakets erläutert.

Mit diesem Element werden alle Formen innerhalb einer Folie angegeben. Dies schließt alle Formen ein, ob gruppiert oder nicht, auf die in einer bestimmten Folie verwiesen werden kann. Da die meisten Objekte innerhalb einer Folie Formen sind, wird hiermit ein Großteil des Inhalts einer Folie dargestellt. Text und Effekte werden an Formen angefügt, die innerhalb des spTree-Elements enthalten sind.

[Ein Beispiel: Stellen Sie sich die folgende PresentationML-Folie vor:

<p:sld>
  <p:cSld>
    <p:spTree>
      <p:nvGrpSpPr>
      ..
      </p:nvGrpSpPr>
      <p:grpSpPr>
      ..
      </p:grpSpPr>
      <p:sp>
      ..
      </p:sp>
    </p:spTree>
  </p:cSld>
  ..
</p:sld>

Im Beispiel oben werden alle Formeigenschaften für diese Folie von der Formstruktur angegeben. Ende des Beispiels]

© ISO/IEC29500: 2008.

In der folgenden Tabelle sind die untergeordneten Elemente der Formstruktur jeweils mit einer Beschreibung aufgeführt.

Element

Beschreibung

cxnSp

Verbindungsform

extLst

Erweiterungsliste mit Änderungskennzeichen

graphicFrame

Grafikrahmen

grpSp

Gruppenform

grpSpPr

Gruppenformeigenschaften

nvGrpSpPr

Nicht sichtbare Eigenschaften für eine Gruppenform

pic

Bild

sp

Form

Das folgende XML-Schemafragment definiert den Inhalt dieses Elements.

<complexType name="CT_GroupShape">
   <sequence>
       <element name="nvGrpSpPr" type="CT_GroupShapeNonVisual" minOccurs="1" maxOccurs="1"/>
       <element name="grpSpPr" type="a:CT_GroupShapeProperties" minOccurs="1" maxOccurs="1"/>
       <choice minOccurs="0" maxOccurs="unbounded">
          <element name="sp" type="CT_Shape"/>
          <element name="grpSp" type="CT_GroupShape"/>
          <element name="graphicFrame" type="CT_GraphicalObjectFrame"/>
          <element name="cxnSp" type="CT_Connector"/>
          <element name="pic" type="CT_Picture"/>
       </choice>
       <element name="extLst" type="CT_ExtensionListModify" minOccurs="0" maxOccurs="1"/>
   </sequence>
</complexType>

Funktionsweise des Beispielcodes

Nach dem Öffnen der Präsentationsdatei für den Lese-/Schreibzugriff in der using-Anweisung ruft der Code den Präsentationsteil aus dem Präsentationsdokument ab. Dann werden die Beziehungs-ID der ersten Folie und der Folienteil aus der Beziehungs-ID abgerufen.

Hinweis

Die Testdatei muss eine gefüllte Form als erste Form auf der ersten Folie besitzen.

using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
    // Get the relationship ID of the first slide.
    PresentationPart part = ppt.PresentationPart;
    OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
    string relId = (slideIds[0] as SlideId).RelationshipId;

    // Get the slide part from the relationship ID.
    SlidePart slide = (SlidePart)part.GetPartById(relId);
Using ppt As PresentationDocument = PresentationDocument.Open(docName, True)

    ' Get the relationship ID of the first slide.
    Dim part As PresentationPart = ppt.PresentationPart
    Dim slideIds As OpenXmlElementList = part.Presentation.SlideIdList.ChildElements
    Dim relId As String = CType(slideIds(0), SlideId).RelationshipId

    ' Get the slide part from the relationship ID.
    Dim slide As SlidePart = CType(part.GetPartById(relId), SlidePart)

Der Code ruft dann die Formstruktur mit der Form ab, deren Füllfarbe geändert werden soll, und es wird die erste Form in der Formstruktur abgerufen. Dann werden das Format der Form und der Füllverweis des Formats abgerufen. Anschließend wird der Form eine neue Füllfarbe zugewiesen. Schließlich wird die geänderte Präsentation gespeichert.

if (slide != null)
{
    // Get the shape tree that contains the shape to change.
    ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;

    // Get the first shape in the shape tree.
    Shape shape = tree.GetFirstChild<Shape>();

    if (shape != null)
    {
        // Get the style of the shape.
        ShapeStyle style = shape.ShapeStyle;

        // Get the fill reference.
        Drawing.FillReference fillRef = style.FillReference;

        // Set the fill color to SchemeColor Accent 6;
        fillRef.SchemeColor = new Drawing.SchemeColor();
        fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6;

        // Save the modified slide.
        slide.Slide.Save();
    }
}
If (Not (slide) Is Nothing) Then

    ' Get the shape tree that contains the shape to change.
    Dim tree As ShapeTree = slide.Slide.CommonSlideData.ShapeTree

    ' Get the first shape in the shape tree.
    Dim shape As Shape = tree.GetFirstChild(Of Shape)()

    If (Not (shape) Is Nothing) Then

        ' Get the style of the shape.
        Dim style As ShapeStyle = shape.ShapeStyle

        ' Get the fill reference.
        Dim fillRef As Drawing.FillReference = style.FillReference

        ' Set the fill color to SchemeColor Accent 6;
        fillRef.SchemeColor = New Drawing.SchemeColor
        fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6

        ' Save the modified slide.
        slide.Slide.Save()
    End If
End If

Beispielcode

Es folgt der vollständige Beispielcode, den Sie zum Ändern der Füllfarbe einer Form in einer Präsentation verwenden können. Sie können in Ihrem Programm die SetPPTShapeColor-Methode zum Ändern der Füllfarbe in der Datei Myppt3.pptx aufrufen, indem Sie den folgenden Aufruf verwenden.

string docName = @"C:\Users\Public\Documents\Myppt3.pptx";
SetPPTShapeColor(docName);
Dim docName As String = "C:\Users\Public\Documents\Myppt3.pptx"
SetPPTShapeColor(docName)

Sehen Sie sich nach Ausführung des Programms die Datei Myppt3.pptx mit der geänderten Füllfarbe an.

// Change the fill color of a shape.
// The test file must have a filled shape as the first shape on the first slide.
public static void SetPPTShapeColor(string docName)
{
    using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
    {
        // Get the relationship ID of the first slide.
        PresentationPart part = ppt.PresentationPart;
        OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
        string relId = (slideIds[0] as SlideId).RelationshipId;

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

        if (slide != null)
        {
            // Get the shape tree that contains the shape to change.
            ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;

            // Get the first shape in the shape tree.
            Shape shape = tree.GetFirstChild<Shape>();

            if (shape != null)
            {
                // Get the style of the shape.
                ShapeStyle style = shape.ShapeStyle;

                // Get the fill reference.
                Drawing.FillReference fillRef = style.FillReference;

                // Set the fill color to SchemeColor Accent 6;
                fillRef.SchemeColor = new Drawing.SchemeColor();
                fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6;

                // Save the modified slide.
                slide.Slide.Save();
            }
        }
    }
}
' Change the fill color of a shape.
' The test file must have a filled shape as the first shape on the first slide.
Public Sub SetPPTShapeColor(ByVal docName As String)
    Using ppt As PresentationDocument = PresentationDocument.Open(docName, True)
        ' Get the relationship ID of the first slide.
        Dim part As PresentationPart = ppt.PresentationPart
        Dim slideIds As OpenXmlElementList = part.Presentation.SlideIdList.ChildElements
        Dim relId As String = TryCast(slideIds(0), SlideId).RelationshipId

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

        If slide IsNot Nothing Then
            ' Get the shape tree that contains the shape to change.
            Dim tree As ShapeTree = slide.Slide.CommonSlideData.ShapeTree

            ' Get the first shape in the shape tree.
            Dim shape As Shape = tree.GetFirstChild(Of Shape)()

            If shape IsNot Nothing Then
                ' Get the style of the shape.
                Dim style As ShapeStyle = shape.ShapeStyle

                ' Get the fill reference.
                Dim fillRef As Drawing.FillReference = style.FillReference

                ' Set the fill color to SchemeColor Accent 6;
                fillRef.SchemeColor = New Drawing.SchemeColor()
                fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6

                ' Save the modified slide.
                slide.Slide.Save()
            End If
        End If
    End Using
End Sub

Siehe auch

Referenz

Class Library Reference