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:
// The path to the documents directory.
const String OutPath = L"../out/SetSlideBackgroundNormal_out.pptx";
// Creates an instance of the Presentation class
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Sets the background color for the first ISlide to Blue
pres->get_Slides()->idx_get(0)->get_Background()->set_Type(BackgroundType::OwnBackground);
pres->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->set_FillType(FillType::Solid);
pres->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Blue());
//Writes the presentation to disk
pres->Save(OutPath, Aspose::Slides::Export::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:
// The path to the documents directory.
const String OutPath = L"../out/SetSlideBackgroundMaster_out.pptx";
// Creates an instance of the Presentation class
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Sets the background color for the Master ISlide to Forest Green
pres->get_Masters()->idx_get(0)->get_Background()->set_Type(BackgroundType::OwnBackground);
pres->get_Masters()->idx_get(0)->get_Background()->get_FillFormat()->set_FillType(FillType::Solid);
pres->get_Masters()->idx_get(0)->get_Background()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_ForestGreen());
//Writes the presentation to disk
pres->Save(OutPath, Aspose::Slides::Export::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
auto pres = System::MakeObject<Presentation>(u"SetBackgroundToGradient.pptx");
// Apply Gradient effect to the Background
pres->get_Slides()->idx_get(0)->get_Background()->set_Type(BackgroundType::OwnBackground);
pres->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->set_FillType(FillType::Gradient);
pres->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->get_GradientFormat()->set_TileFlip(TileFlip::FlipBoth);
// Writes the presentation to disk
pres->Save(u"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:
// The path to the documents directory.
const String templatePath = L"../templates/SetImageAsBackground.pptx";
const String imagePath = L"../templates/Tulips.jpg";
const String outPath = L"../out/ContentBG_Img_out.pptx";
// Creates an instance of the Presentation class
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Sets conditions for background image
pres->get_Slides()->idx_get(0)->get_Background()->set_Type(BackgroundType::OwnBackground);
pres->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->set_FillType(FillType::Picture);
pres->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->get_PictureFillFormat()->set_PictureFillMode(PictureFillMode::Stretch);
// Loads the image
auto image = Images::FromFile(imagePath);
// Adds image to presentation's images collection
SharedPtr<IPPImage> imgx = pres->get_Images()->AddImage(image);
pres->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->get_PictureFillFormat()->get_Picture()->set_Image(imgx);
// Writes the presentation to disk
pres->Save(outPath, Aspose::Slides::Export::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:
int32_t transparencyValue = 30;
// for example
// Gets a collection of picture transform operations
auto imageTransform = slide->get_Background()->get_FillFormat()->get_PictureFillFormat()->get_Picture()->get_ImageTransform();
// Finds a transparency effect with fixed percentage.
System::SharedPtr<AlphaModulateFixed> transparencyOperation;
for (auto&& operation : imageTransform)
{
if (System::ObjectExt::Is<AlphaModulateFixed>(operation))
{
transparencyOperation = System::ExplicitCast<AlphaModulateFixed>(operation);
break;
}
}
// Sets the new transparency value.
if (transparencyOperation == nullptr)
{
imageTransform->AddAlphaModulateFixedEffect(100.0f - transparencyValue);
}
else
{
transparencyOperation->set_Amount(100.0f - 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
const String templatePath = u"../templates/SamplePresentation.pptx";
auto pres = System::MakeObject<Presentation>(templatePath);
System::SharedPtr<IBackgroundEffectiveData> effBackground = pres->get_Slides()->idx_get(0)->CreateBackgroundEffective();
if (effBackground->get_FillFormat()->get_FillType() == Aspose::Slides::FillType::Solid)
{
System::Console::WriteLine(System::String(u"Fill color: ") + effBackground->get_FillFormat()->get_SolidFillColor());
}
else
{
System::Console::WriteLine(System::String(u"Fill type: ") + System::ObjectExt::ToString(effBackground->get_FillFormat()->get_FillType()));
}