When we create PowerPoint documents, the default slide of the system is blank background. Many times, we need to customize the slide background to achieve beautiful document effect. In the following example, we will introduce the method of setting the background for PowerPoint slides, mainly including the following three parts:
- Add solid background
- Add gradient background
- Add picture as background
Tools needed
- Free fire.presentation for. Net version 3.3 (Community Edition)
Sample code (for reference)
Step 1: add the following using instruction
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing;
Step 2: create a document
Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx");
Step 3: add a solid background
//Set the background fill mode of the document to solid fill ppt.Slides[0].SlideBackground.Type = BackgroundType.Custom; ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Solid; ppt.Slides[0].SlideBackground.Fill.SolidColor.Color = Color.Pink;
Step 4: add a gradient background color
//Set the background fill mode of the document to gradient fill ppt.Slides[1].SlideBackground.Type = BackgroundType.Custom; ppt.Slides[1].SlideBackground.Fill.FillType = FillFormatType.Gradient; ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(0f, KnownColors.Yellow); ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(1f, KnownColors.Orange);
Step 5: add picture as background
//Set slide background color to picture background ppt.Slides[2].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom; ppt.Slides[2].SlideBackground.Fill.FillType = FillFormatType.Picture; ppt.Slides[2].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch; //Load picture as slide background Image img = Image.FromFile("green.png"); IImageData image = ppt.Images.Append(img); ppt.Slides[2].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;
Step 6: save the file
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx");
After the code is completed, debug and run the program to generate a file, as follows:
All codes:
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace AddBackground_PPT { class Program { static void Main(string[] args) { //instantiation Presentation Class loading PowerPoint File Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx"); //Set the background fill mode of the document to solid fill ppt.Slides[0].SlideBackground.Type = BackgroundType.Custom; ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Solid; ppt.Slides[0].SlideBackground.Fill.SolidColor.Color = Color.Pink; //Set the background fill mode of the document to gradient fill ppt.Slides[1].SlideBackground.Type = BackgroundType.Custom; ppt.Slides[1].SlideBackground.Fill.FillType = FillFormatType.Gradient; ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(0f, KnownColors.Yellow); ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(1f, KnownColors.Orange); //Set slide background color to picture background ppt.Slides[2].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom; ppt.Slides[2].SlideBackground.Fill.FillType = FillFormatType.Picture; ppt.Slides[2].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch; //Load picture as slide background Image img = Image.FromFile("green.png"); IImageData image = ppt.Images.Append(img); ppt.Slides[2].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image; //Save and open document ppt.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } } }
This article is finished.
If you need to reprint, please indicate the source!!