A slide master in PowerPoint preserves fixed styles, such as background, title, theme color, etc., which can be inherited by other slides. Using the master in PowerPoint can ensure the consistency of style and avoid repeated work. In this article, I am going to show you how to apply two different slide masters to different slides in a single PowerPoint presention, by using Spire.Presentation for Java.
Sample document
Add Spire.Presentation.jar as dependency
Method 1: Download Spire.Presentation for Java pack, unzip it and you’ll get Spire.Presentation.jar file from the “lib” folder. Import the jar file in your project as a dependency.
Method 2: If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.presentation</artifactId>
<version>3.5.4/version>
</dependency>
</dependencies>
Using the code
import com.spire.presentation.*;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
public class ApplyTwoMastersInPPT {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
//Load the sample powerpoint document
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");
//Get the first slide master
IMasterSlide first_master = presentation.getMasters().get(0);
//Create another slide master based on the first one
presentation.getMasters().appendSlide(first_master);
IMasterSlide second_master = presentation.getMasters().get(1);
//Set different background images for the two masters
String pic1 = "C:\\Users\\Administrator\\Desktop\\image1.jpg";
String pic2 = "C:\\Users\\Administrator\\Desktop\\image2.jpg";
BufferedImage image = ImageIO.read(new FileInputStream(pic1));
IImageData imageData = presentation.getImages().append(image);
first_master.getSlideBackground().setType(BackgroundType.CUSTOM);
first_master.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
first_master.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
first_master.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
image = ImageIO.read(new FileInputStream(pic2));
imageData = presentation.getImages().append(image);
second_master.getSlideBackground().setType(BackgroundType.CUSTOM);
second_master.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
second_master.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
second_master.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
//Apply the first master along with the layout to the first slide
presentation.getSlides().get(0).setLayout(first_master.getLayouts().get(6));
//Apply the second master along with the layout to the rest slides
for (int i = 1; i < presentation.getSlides().getCount(); i++)
{
presentation.getSlides().get(i).setLayout(second_master.getLayouts().get(6));
}
//Save to file
presentation.saveToFile("ApplyMultiMasters.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}
Top comments (0)