In this article, you will learn how to dynamically set the background of slides in PowerPoint presentations using Java. More precisely, the article will cover how to set the background of normal slides as well as the master slide.
- Java API to Set Slide Background in PowerPoint
- Set Background Color of Normal Slides
- Set Background Color of Master Slide
- Gradient as Slide Background Color
- Set Image as Slide Background
Java API to Set Slide Background in PowerPoint
To set the background of slides in PPTX/PPT presentations, we will use Aspose.Slides for Java. The API lets you create, manipulate and convert PowerPoint and OpenOffice presentations from within the Java applications. You can either download the API or install it in your Maven-based applications using the following configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>21.8</version>
<classifier>jdk16</classifier>
</dependency>
Set Background Color of Normal Slides in Java
The following are the steps to set the background color of the normal slides in a PowerPoint presentation using Java.
- First, load the PowerPoint presentation using Presentation class.
- Then, set the background of the desired slide by specifying its index, e.g. background type, color, fill type, etc.
- Finally, save the updated presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to set the background of a slide in a PowerPoint presentation.
The screenshot of the slide before setting the background is given below.
The following is the PowerPoint slide after setting the background.
Set Background Color of Master Slide in Java
You can also set the background of the master slide that will affect all the slides in the presentation. The following are the steps to change the background color of the master slide.
- First, load the PowerPoint presentation using Presentation class.
- Then, set the background of the master slide, e.g. background type, color, fill type, etc.
- Finally, save the updated presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to change the background of the master slide in PowerPoint.
Set Gradient Background Color of Slides
The following are the steps to set a gradient background color of the slides in a PowerPoint presentation.
- First, load the PowerPoint presentation using Presentation class.
- Set Presentation.getSlides().get_Item(0).getBackground().setType() to FillType.Gradient.
- Set TileFlip to TileFlip.FlipBoth.
- Finally, save the updated presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to set the gradient background color of the slides in PowerPoint.
The following screenshot shows the gradient background of the slide.
Set Image as Slide Background using Java
The following are the steps to set an image as a slide background using Java.
- First, load the PowerPoint presentation using Presentation class.
- Set FillType to FillType.Picture.
- Set PictureFillMode to PictureFillMode.Stretch.
- Add image to the collection of the presentation and get its reference into an IPPImage object.
- Set image as background using setImage(IPPImage) method.
- Finally, save the updated presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to set an image as the background of slides in a PowerPoint presentation.
Get a Free API License
You can use Aspose.Slides for Java without evaluation limitations by requesting a temporary license.
Conclusion
In this article, you have learned how to set the background of slides in PowerPoint PPTX or PPT using Java. Furthermore, you have seen how to set gradient or image background of the PowerPoint presentations. You can visit the documentation to explore other features of Aspose.Slides for Java. Also, you can feel free to let us know about your queries via our forum.