Presentation Background

Overview

In this topic, we will see that how can we set the background color of a slide. We know that Aspose.Slides for .NET may contain two types of slides: Master Slide & Normal Slide. It is possible to change the background colors of both types of slides, which will be explained in this topic.

Setting Background Color for Master Slides

We know that Aspose.Slides for .NET may contain two types of slides: Master Slide & Normal Slide. It is possible to change the background colors of both types of slides. Master Slide is like a template that contains all formatting settings, which are applied on all other normal slides contained inside the presentation. It means that if you change the background color of the master slide, all normal slides in the presentation would receive the same background color settings. Please follow the steps below to change the background color of the master slide:

  1. Create an instance of Presentation class.
  2. Set the Background Type of the Slide to OwnBackground.
  3. Set the FillType of the Slide Background to Solid.
  4. Set the Background Color of the Master Slide of the presentation to any desired color using the SolidFillColor.Color property exposed by FillFormat object.
  5. Write the modified presentation as a presentation file.
// Instantiate the Presentation class that represents the presentation file
using (Presentation pres = new Presentation())
{

    // Set the background color of the Master ISlide to Forest Green
    pres.Masters[0].Background.Type = BackgroundType.OwnBackground;
    pres.Masters[0].Background.FillFormat.FillType = FillType.Solid;
    pres.Masters[0].Background.FillFormat.SolidFillColor.Color = Color.ForestGreen;

    // Write the presentation to disk
    pres.Save("SetSlideBackgroundMaster_out.pptx", SaveFormat.Pptx);

}

Setting Background Color to Normal Slides

A Normal Slide is the one which inherits its format settings from the master slide. If you want to modify its background settings, you would have to modify the slide settings. Please follow the steps below to perform this task:

  • Create an instance of Presentation class.
  • Set the Background Type of the Slide to OwnBackground.
  • Set the FillType of the Slide Background to Solid.
  • Set the Background Color of the Normal Slide of the presentation to any desired color using the SolidFillColor.Color property exposed by FillFormat object.
  • Write the modified presentation as a presentation file.
// Instantiate the Presentation class that represents the presentation file
using (Presentation pres = new Presentation())
{

    // Set the background color of the first ISlide to Blue
    pres.Slides[0].Background.Type = BackgroundType.OwnBackground;
    pres.Slides[0].Background.FillFormat.FillType = FillType.Solid;
    pres.Slides[0].Background.FillFormat.SolidFillColor.Color = Color.Blue;
    pres.Save("ContentBG_out.pptx", SaveFormat.Pptx);
}

Setting Gradient Background Color for Slides

Gradient is a graphic effect consisting of a gradual change in color. It is great for creating depth and highlights to sections of the images. It is possible to apply gradient effect on the background of a slide using Aspose.Slides for .NET that will be explained in the remaining discussion of this topic.

To apply the simple gradient effect on the background of a slide using Aspose.Slides for .NET, please follow the steps below:

  • Create an instance of Presentation class.
  • Set the Background Type of the Slide to OwnBackground.
  • Set the FillType of the Slide Background to Gradient.
  • Apply any desired gradient effect from the available options provided by GradientFormatEx object.
  • Write the modified presentation file.
// Instantiate the Presentation class that represents the presentation file
using (Presentation pres = new Presentation("SetBackgroundToGradient.pptx"))
{

    // Apply Gradiant effect to the Background
    pres.Slides[0].Background.Type = BackgroundType.OwnBackground;
    pres.Slides[0].Background.FillFormat.FillType = FillType.Gradient;
    pres.Slides[0].Background.FillFormat.GradientFormat.TileFlip = TileFlip.FlipBoth;

    //Write the presentation to disk
    pres.Save("ContentBG_Grad_out.pptx", SaveFormat.Pptx);
}

Setting Images as Background for Slides

Sometimes, developers may need to use an image as the background of the slide. To fulfill such development needs, Aspose.Slides for .NET also allows filling the slide background with any image.

To use an image as the background of a slide using Aspose.Slides for .NET, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Set the Background Type of the Slide to OwnBackground.
  3. Set the FillType of the Slide Background FillFormat to Picture.
  4. Set the PictureFillMode using the options provided by PictureFillMode enum.
  5. Instantiate Image class with an image that can be used as source picture for the Slide Background using PictureFillFormat.Picture.Image.
  6. Write the modified presentation file.
// Instantiate the Presentation class that represents the presentation file
using (Presentation pres = new Presentation("SetImageAsBackground.pptx"))
{

    // Set the background with Image
    pres.Slides[0].Background.Type = BackgroundType.OwnBackground;
    pres.Slides[0].Background.FillFormat.FillType = FillType.Picture;
    pres.Slides[0].Background.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;

    // Set the picture
    System.Drawing.Image img = (System.Drawing.Image)new Bitmap(dataDir + "Tulips.jpg");

    // Add image to presentation's images collection
    IPPImage imgx = pres.Images.AddImage(img);

    pres.Slides[0].Background.FillFormat.PictureFillFormat.Picture.Image = imgx;

    // Write the presentation to disk
    pres.Save("ContentBG_Img_out.pptx", SaveFormat.Pptx);
}

Getting Effective Background Values of Slides

Aspose.Slides.IBackgroundEffectiveData interface and its implementation by Aspose.Slides.BackgroundEffectiveData class have been added. They represent effective background of slide and contain information about effective fill format and effective effect format.

CreateBackgroundEffective method has been added to IBaseSlide interface and BaseSlide class. This method allows to get effective values for slides background.

The following code snippet shows how to get effective background values of slide.

// Instantiate the Presentation class that represents the presentation file
Presentation pres = new Presentation("SamplePresentation.pptx");

IBackgroundEffectiveData effBackground = pres.Slides[0].Background.GetEffective();

if (effBackground.FillFormat.FillType == FillType.Solid)
    Console.WriteLine("Fill color: " + effBackground.FillFormat.SolidFillColor);
else
    Console.WriteLine("Fill type: " + effBackground.FillFormat.FillType);