Presentation Background
Solid colors, gradient colors, and pictures are often used as background images for slides. You can set the background either for a normal slide (single slide) or master slide (several slides at once).
Set Solid Color as Background for Normal Slide
Aspose.Slides allows you to set a solid color as the background for a specific slide in a presentation (even if that presentation contains a master slide). The background change affects only the selected slide.
- Create an instance of the Presentation class.
- Set the BackgroundType enum for the slide to
OwnBackground
. - Set the FillType enum for the slide background to
Solid
. - Use the SolidFillColor property exposed by FillFormat to specify a solid color for the background.
- Save the modified presentation.
This C# code shows you how to set a solid color (blue) as the background for a normal slide:
// Creates an instance of the Presentation class
using (Presentation pres = new Presentation())
{
// Sets the background color for 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;
// Writes the presentation to disk
pres.Save("ContentBG_out.pptx", SaveFormat.Pptx);
}
Set Solid Color as Background for Master Slide
Aspose.Slides allows you to set a solid color as the background for the master slide in a presentation. The master slide acts as a template that contains and controls formatting settings for all slides. Therefore, when you select a solid color as the background for the master slide, that new background will be used for all slides.
- Create an instance of the Presentation class.
- Set the BackgroundType enum for the master slide (
Masters
) toOwnBackground
. - Set the FillType enum for the master slide background to
Solid
. - Use the SolidFillColor property exposed by FillFormat to specify a solid color for the background.
- Save the modified presentation.
This C# code shows you how to set a solid color (forest green) as the background for a master slide in a presentation:
// Creates an instance of the Presentation class
using (Presentation pres = new Presentation())
{
// Sets the background color for 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;
// Writes the presentation to disk
pres.Save("SetSlideBackgroundMaster_out.pptx", SaveFormat.Pptx);
}
Set Gradient Color as Background for Slide
A gradient is a graphical effect based on a gradual change in color. Gradient colors, when used as backgrounds for slides, make presentations looks artistic and professional. Aspose.Slides allows you to set a gradient color as the background for slides in presentations.
- Create an instance of the Presentation class.
- Set the BackgroundType enum for the slide to
OwnBackground
. - Set the FillType enum for the master slide background to
Gradient
. - Use the GradientFormat property exposed by FillFormat to specify your preferred gradient setting.
- Save the modified presentation.
This C# code shows you how to set a gradient color as the background for a slide:
// Creates an instance of the Presentation class
using (Presentation pres = new Presentation("SetBackgroundToGradient.pptx"))
{
// Apply Gradient 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;
// Writes the presentation to disk
pres.Save("ContentBG_Grad_out.pptx", SaveFormat.Pptx);
}
Set Image as Background for Slide
Besides solid colors and gradient colors, Aspose.Slides also allows you to set images as the background for slides in presentations.
- Create an instance of the Presentation class.
- Set the BackgroundType enum for the slide to
OwnBackground
. - Set the FillType enum for the master slide background to
Picture
. - Load the image you want to use as the slide background.
- Add the image to the presentation’s image collection.
- Use the PictureFillFormat property exposed by FillFormat to set the image as the background.
- Save the modified presentation.
This C# code shows you how to set an image as the background for a slide:
// Creates an instance of the Presentation class
using (Presentation pres = new Presentation("SetImageAsBackground.pptx"))
{
// Sets conditions for background 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;
// Loads an image and adds it to the presentation's image collection
IImage image = Images.FromFile("Tulips.jpg");
IPPImage ppImage = pres.Images.AddImage(image);
image.Dispose();
pres.Slides[0].Background.FillFormat.PictureFillFormat.Picture.Image = ppImage;
// Writes the presentation to disk
pres.Save("ContentBG_Img_out.pptx", SaveFormat.Pptx);
}
Change Transparency of Background Image
You may want to adjust the transparency of a slide’s background image to make the contents of the slide stand out. This C# code shows you how to change the transparency for a slide background image:
var transparencyValue = 30; // for example
// Gets a collection of picture transform operations
var imageTransform = slide.Background.FillFormat.PictureFillFormat.Picture.ImageTransform;
// Finds a transparency effect with fixed percentage.
var transparencyOperation = null as AlphaModulateFixed;
foreach (var operation in imageTransform)
{
if (operation is AlphaModulateFixed alphaModulateFixed)
{
transparencyOperation = alphaModulateFixed;
break;
}
}
// Sets the new transparency value.
if (transparencyOperation == null)
{
imageTransform.AddAlphaModulateFixedEffect(100 - transparencyValue);
}
else
{
transparencyOperation.Amount = (100 - transparencyValue);
}
Get Value of Slide Background
Aspose.Slides provides the IBackgroundEffectiveData interface to allow you to get the effective values of slide backgrounds. This interface contains information on the effective FillFormat and effective EffectFormat.
Using the Background property from the BaseSlide class, you can get the effective value for a slide background.
This C# code shows you how to get a slide’s effective background value:
// Creates an instance of the Presentation class
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);