.NET でプレゼンテーションの背景を管理する
概要
単色、グラデーション、画像はスライドの背景として一般的に使用されます。通常のスライド(単一のスライド)またはマスタースライド(複数のスライドに一度に適用)に対して背景を設定できます。

通常のスライドの単色背景を設定する
Aspose.Slides では、プレゼンテーション内の特定のスライドの背景として単色を設定できます。プレゼンテーションがマスタースライドを使用している場合でも、変更は選択したスライドのみに適用されます。
- Presentation クラスのインスタンスを作成します。
- スライドの BackgroundType を
OwnBackgroundに設定します。 - スライドの背景 FillType を
Solidに設定します。 - FillFormat の SolidFillColor プロパティを使用して単色背景色を指定します。
- 変更されたプレゼンテーションを保存します。
以下の C# の例は、通常のスライドの背景として青色の単色を設定する方法を示しています。
// Presentation クラスのインスタンスを作成します。
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
// スライドの背景色を青に設定します。
slide.Background.Type = BackgroundType.OwnBackground;
slide.Background.FillFormat.FillType = FillType.Solid;
slide.Background.FillFormat.SolidFillColor.Color = Color.Blue;
// プレゼンテーションをディスクに保存します。
presentation.Save("SolidColorBackground.pptx", SaveFormat.Pptx);
}
マスタースライドの単色背景を設定する
Aspose.Slides では、プレゼンテーションのマスタースライドの背景として単色を設定できます。マスタースライドはすべてのスライドの書式設定を制御するテンプレートとして機能するため、マスタースライドの背景に単色を選択すると、すべてのスライドに適用されます。
- Presentation クラスのインスタンスを作成します。
- マスタースライドの BackgroundType(
masters経由)をOwnBackgroundに設定します。 - マスタースライドの背景 FillType を
Solidに設定します。 - SolidFillColor を使用して単色背景色を指定します。
- 変更されたプレゼンテーションを保存します。
以下の C# の例は、マスタースライドの背景として単色(フォレストグリーン)を設定する方法を示しています。
// Presentation クラスのインスタンスを作成します。
using (Presentation presentation = new Presentation())
{
IMasterSlide masterSlide = presentation.Masters[0];
// マスタースライドの背景色をフォレストグリーンに設定します。
masterSlide.Background.Type = BackgroundType.OwnBackground;
masterSlide.Background.FillFormat.FillType = FillType.Solid;
masterSlide.Background.FillFormat.SolidFillColor.Color = Color.ForestGreen;
// プレゼンテーションをディスクに保存します。
presentation.Save("MasterSlideBackground.pptx", SaveFormat.Pptx);
}
スライドのグラデーション背景を設定する
グラデーションは、色が徐々に変化することによって作成されるグラフィック効果です。スライドの背景として使用すると、プレゼンテーションがより芸術的かつプロフェッショナルに見えます。Aspose.Slides では、スライドの背景としてグラデーションカラーを設定できます。
- Presentation クラスのインスタンスを作成します。
- スライドの BackgroundType を
OwnBackgroundに設定します。 - スライドの背景 FillType を
Gradientに設定します。 - FillFormat の GradientFormat プロパティを使用して、好みのグラデーション設定を構成します。
- 変更されたプレゼンテーションを保存します。
以下の C# の例は、スライドの背景としてグラデーションカラーを設定する方法を示しています。
// Presentation クラスのインスタンスを作成します。
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
// 背景にグラデーション効果を適用します。
slide.Background.Type = BackgroundType.OwnBackground;
slide.Background.FillFormat.FillType = FillType.Gradient;
slide.Background.FillFormat.GradientFormat.TileFlip = TileFlip.FlipBoth;
// プレゼンテーションをディスクに保存します。
presentation.Save("GradientBackground.pptx", SaveFormat.Pptx);
}
スライドの背景に画像を設定する
単色およびグラデーションの塗りつぶしに加えて、Aspose.Slides では画像をスライドの背景として使用できます。
- Presentation クラスのインスタンスを作成します。
- スライドの BackgroundType を
OwnBackgroundに設定します。 - スライドの背景 FillType を
Pictureに設定します。 - スライドの背景として使用する画像を読み込みます。
- 画像をプレゼンテーションの画像コレクションに追加します。
- FillFormat の PictureFillFormat プロパティを使用して画像を背景として割り当てます。
- 変更されたプレゼンテーションを保存します。
以下の C# の例は、スライドの背景として画像を設定する方法を示しています。
// Presentation クラスのインスタンスを作成します。
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
// バックグラウンド画像のプロパティを設定します。
slide.Background.Type = BackgroundType.OwnBackground;
slide.Background.FillFormat.FillType = FillType.Picture;
slide.Background.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
// 画像を読み込みます。
IImage image = Images.FromFile("Tulips.jpg");
// 画像をプレゼンテーションの画像コレクションに追加します。
IPPImage ppImage = presentation.Images.AddImage(image);
image.Dispose();
slide.Background.FillFormat.PictureFillFormat.Picture.Image = ppImage;
// プレゼンテーションをディスクに保存します。
presentation.Save("ImageAsBackground.pptx", SaveFormat.Pptx);
}
以下のコードサンプルは、背景の塗りつぶしタイプをタイル状の画像に設定し、タイル設定プロパティを変更する方法を示しています。
using (Presentation presentation = new Presentation())
{
ISlide firstSlide = presentation.Slides[0];
IBackground background = firstSlide.Background;
background.Type = BackgroundType.OwnBackground;
background.FillFormat.FillType = FillType.Picture;
IPPImage ppImage;
using (IImage newImage = Aspose.Slides.Images.FromFile("image.png"))
ppImage = presentation.Images.AddImage(newImage);
// 背景塗りつぶしに使用する画像を設定します。
IPictureFillFormat backPictureFillFormat = background.FillFormat.PictureFillFormat;
backPictureFillFormat.Picture.Image = ppImage;
// ピクチャーフィルモードをタイルに設定し、タイルプロパティを調整します。
backPictureFillFormat.PictureFillMode = PictureFillMode.Tile;
backPictureFillFormat.TileOffsetX = 15f;
backPictureFillFormat.TileOffsetY = 15f;
backPictureFillFormat.TileScaleX = 46f;
backPictureFillFormat.TileScaleY = 87f;
backPictureFillFormat.TileAlignment = RectangleAlignment.Center;
backPictureFillFormat.TileFlip = TileFlip.FlipY;
presentation.Save("TileBackground.pptx", SaveFormat.Pptx);
}
背景画像の透明度を変更する
スライドの背景画像の透明度を調整して、スライドの内容を際立たせたい場合があります。以下の C# コードは、スライド背景画像の透明度を変更する方法を示しています。
var transparencyValue = 30; // 例として。
// ピクチャー変換操作のコレクションを取得します。
var imageTransform = slide.Background.FillFormat.PictureFillFormat.Picture.ImageTransform;
// 既存の固定パーセンテージ透明度エフェクトを検索します。
var transparencyOperation = null as IAlphaModulateFixed;
foreach (var operation in imageTransform)
{
if (operation is IAlphaModulateFixed alphaModulateFixed)
{
transparencyOperation = alphaModulateFixed;
break;
}
}
// 新しい透明度の値を設定します。
if (transparencyOperation == null)
{
imageTransform.AddAlphaModulateFixedEffect(100 - transparencyValue);
}
else
{
transparencyOperation.Amount = (100 - transparencyValue);
}
スライドの背景値を取得する
Aspose.Slides は、スライドの実効背景値を取得するための IBackgroundEffectiveData インターフェイスを提供します。このインターフェイスは、実際の FillFormat と EffectFormat を公開します。
BaseSlide クラスの background プロパティを使用して、スライドの実効背景を取得できます。
以下の C# の例は、スライドの実効背景値を取得する方法を示しています。
// Presentation クラスのインスタンスを作成します。
using (Presentation presentation = new Presentation("Sample.pptx"))
{
ISlide slide = presentation.Slides[0];
// マスター、レイアウト、テーマを考慮して実効背景を取得します。
IBackgroundEffectiveData effBackground = slide.Background.GetEffective();
if (effBackground.FillFormat.FillType == FillType.Solid)
Console.WriteLine("Fill color: " + effBackground.FillFormat.SolidFillColor);
else
Console.WriteLine("Fill type: " + effBackground.FillFormat.FillType);
}
よくある質問
カスタム背景をリセットしてテーマ/レイアウトの背景に戻すことはできますか?
はい。スライドのカスタム塗りつぶしを削除すると、背景は対応する layout/master スライド(すなわち theme background)から再び継承されます。
後でプレゼンテーションのテーマを変更した場合、背景はどうなりますか?
スライドが独自の塗りつぶしを持っている場合、その背景は変更されません。背景が layout/master から継承されている場合は、new theme に合わせて更新されます。