プレゼンテーション内の図形の塗りつぶしの色を変更する
このトピックでは、Open XML SDK のクラスを使用して、プレゼンテーションの最初のスライドの図形の塗りつぶしの色をプログラムで変更する方法について説明します。
Presentation オブジェクトの取得
Open XML SDK では、 PresentationDocument
クラスはプレゼンテーション ドキュメント パッケージを表します。 プレゼンテーション ドキュメントを操作するには、まず PresentationDocument
クラスのインスタンスを作成してから、そのインスタンスを操作します。 ドキュメントからクラス インスタンスを作成するには、ファイル パスを使用する Open
メソッドを呼び出し、2 番目のパラメーターとしてブール値を使用してドキュメントを編集可能にするかどうかを指定します。 読み取り/書き込みのためにドキュメントを開くには、次のusing
ステートメントに示すように、このパラメーターの値true
を指定します。 このコードでは、ファイル パラメーターは開くドキュメントのファイルのパスを表す文字列です。
using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
v3.0.0 以降では、using ステートメントに依存することを優先して、Close() メソッドが削除されました。
これにより、閉じかっこに達したときに、 Dispose() メソッドが自動的に呼び出されます。
using
ステートメントに続くブロックは、using
ステートメントで作成または名前付けされたオブジェクトのスコープを確立します(この場合はppt
。
図形ツリーの構造
PresentationML ドキュメントの基本的なドキュメント構造は、多数のパーツで構成され、その中には Shape Tree (<spTree/>
) 要素があります。
ISO/IEC 29500 仕様の次のテキストでは、PresentationML
パッケージの全体的な形式について説明します。
この要素は、スライド内のすべての図形を指定します。 グループ化されているといないとにかかわらず、指定のスライド上で参照できるすべての図形がここに含まれます。 スライド内のほとんどのオブジェクトが図形であるため、この要素は、スライド内のコンテンツの大部分を表します。 テキストと効果は、
spTree
要素に含まれる図形にアタッチされます。[例: 次の
PresentationML
スライドを検討してください
<p:sld>
<p:cSld>
<p:spTree>
<p:nvGrpSpPr>
..
</p:nvGrpSpPr>
<p:grpSpPr>
..
</p:grpSpPr>
<p:sp>
..
</p:sp>
</p:spTree>
</p:cSld>
..
</p:sld>
上の例では、図形ツリーがこのスライドのすべての図形プロパティを指定しています。 例終わり]
© ISO/IEC 29500: 2016
次の表に、図形ツリーの子要素とそれぞれの説明を示します。
要素 | 説明 |
---|---|
cxnSp | 接続図形 |
extLst | 拡張リストと変更フラグ |
graphicFrame | グラフィック フレーム |
grpSp | グループ図形 |
grpSpPr | グループ図形プロパティ |
nvGrpSpPr | グループ図形の非表示プロパティ |
pic | 画像 |
sp | 図形 |
次の XML スキーマ フラグメントは、この要素のコンテンツを定義しています。
<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>
サンプル コードの動作のしくみ
using
ステートメントで読み取り/書き込みアクセス用のプレゼンテーション ファイルを開いた後、コードはプレゼンテーション ドキュメントからプレゼンテーション パーツを取得します。 最初のスライドのリレーションシップ ID を取得し、そのリレーションシップ ID からスライド パーツを取得します。
注:
テスト ファイルには、最初のスライドの図形が必要です。
// Get the relationship ID of the first slide.
PresentationPart presentationPart = ppt.PresentationPart ?? ppt.AddPresentationPart();
presentationPart.Presentation.SlideIdList ??= new SlideIdList();
SlideId? slideId = presentationPart.Presentation.SlideIdList.GetFirstChild<SlideId>();
if (slideId is not null)
{
string? relId = slideId.RelationshipId;
if (relId is not null)
{
// Get the slide part from the relationship ID.
SlidePart slidePart = (SlidePart)presentationPart.GetPartById(relId);
塗りつぶしの色を変更する図形が含まれる図形ツリーを取得し、図形ツリー内の最初の図形を取得します。 次に、図形の図形プロパティと図形プロパティの塗りつぶし参照を取得し、図形に新しい塗りつぶしの色を割り当てます。 using の内部でファイルを明示的に保存する必要はありません。
// Get or add the shape tree
slidePart.Slide.CommonSlideData ??= new CommonSlideData();
// Get the shape tree that contains the shape to change.
slidePart.Slide.CommonSlideData.ShapeTree ??= new ShapeTree();
// Get the first shape in the shape tree.
Shape? shape = slidePart.Slide.CommonSlideData.ShapeTree.GetFirstChild<Shape>();
if (shape is not null)
{
// Get or add the shape properties element of the shape.
shape.ShapeProperties ??= new ShapeProperties();
// Get or add the fill reference.
Drawing.SolidFill? solidFill = shape.ShapeProperties.GetFirstChild<Drawing.SolidFill>();
if (solidFill is null)
{
shape.ShapeProperties.AddChild(new Drawing.SolidFill());
solidFill = shape.ShapeProperties.GetFirstChild<Drawing.SolidFill>();
}
// Set the fill color to SchemeColor
solidFill!.SchemeColor = new Drawing.SchemeColor() { Val = Drawing.SchemeColorValues.Accent2 };
}
サンプル コード
以下は、プレゼンテーション内の図形の塗りつぶしの色を変更できる完全なサンプル コードです。
// Change the fill color of a shape.
// The test file must have a shape on the first slide.
static void SetPPTShapeColor(string docName)
{
using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
// Get the relationship ID of the first slide.
PresentationPart presentationPart = ppt.PresentationPart ?? ppt.AddPresentationPart();
presentationPart.Presentation.SlideIdList ??= new SlideIdList();
SlideId? slideId = presentationPart.Presentation.SlideIdList.GetFirstChild<SlideId>();
if (slideId is not null)
{
string? relId = slideId.RelationshipId;
if (relId is not null)
{
// Get the slide part from the relationship ID.
SlidePart slidePart = (SlidePart)presentationPart.GetPartById(relId);
// Get or add the shape tree
slidePart.Slide.CommonSlideData ??= new CommonSlideData();
// Get the shape tree that contains the shape to change.
slidePart.Slide.CommonSlideData.ShapeTree ??= new ShapeTree();
// Get the first shape in the shape tree.
Shape? shape = slidePart.Slide.CommonSlideData.ShapeTree.GetFirstChild<Shape>();
if (shape is not null)
{
// Get or add the shape properties element of the shape.
shape.ShapeProperties ??= new ShapeProperties();
// Get or add the fill reference.
Drawing.SolidFill? solidFill = shape.ShapeProperties.GetFirstChild<Drawing.SolidFill>();
if (solidFill is null)
{
shape.ShapeProperties.AddChild(new Drawing.SolidFill());
solidFill = shape.ShapeProperties.GetFirstChild<Drawing.SolidFill>();
}
// Set the fill color to SchemeColor
solidFill!.SchemeColor = new Drawing.SchemeColor() { Val = Drawing.SchemeColorValues.Accent2 };
}
}
}
}
}