The slide master in PowerPoint preserves some fixed styles, such as headline, background, property and so on. Pre-configured unique slide master can be applied in all slides, and we can customize multiple slide masters and apply them to different slides. In this article, I’ll show you how to create slide masters and apply them in a PowerPoint file by using Free Spire.Presentation for Java.
BEFORE START
Download the latest version of Free Spire.Presentation for Java, unzip it and add Spire.Presentation.jar located in the “lib” folder to your Java IDEA.
Besides, 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
Sample 1 Apply One Slide Master in PowerPoint
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.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
public class UniqueSlideMaster {
public static void main(String[] args) throws Exception {
//create a Presentation object and specify the slide
size
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//get the first slide master
IMasterSlide masterSlide =
presentation.getMasters().get(0);
//set the background image of the slide master
String backgroundPic = "C:\\Users\\Test1\\Desktop\\Background.jpg";
BufferedImage image = ImageIO.read(new FileInputStream(backgroundPic));
IImageData imageData =
presentation.getImages().append(image);
masterSlide.getSlideBackground().setType(BackgroundType.CUSTOM);
masterSlide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
masterSlide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
masterSlide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
//add an image (company logo) to slide master
String logo = "C:\\Users\\Test1\\Desktop\\logo.png";
image = ImageIO.read(new FileInputStream(logo));
imageData =
presentation.getImages().append(image);
IEmbedImage imageShape =
masterSlide.getShapes().appendEmbedImage(ShapeType.RECTANGLE,imageData,new Rectangle2D.Float(50,50,200,100));
imageShape.getLine().setFillType(FillFormatType.NONE);
//add some text (company name) to slide master
IAutoShape textShape = masterSlide.getShapes().appendShape(ShapeType.RECTANGLE, new
Rectangle2D.Float((float) presentation.getSlideSize().getSize().getWidth()-220,(float) presentation.getSlideSize().getSize().getHeight()-60,200,30));//Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(ppt.SlideSize.Size.Width-200,
ppt.SlideSize.Size.Height-60, 200, 30));
textShape.getTextFrame().setText("Eagle Media Co., LTD");
textShape.getTextFrame().getTextRange().setFontHeight(20f);
textShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
textShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.blue);
textShape.getTextFrame().getTextRange().getParagraph().setAlignment(TextAlignmentType.CENTER);
textShape.getFill().setFillType(FillFormatType.NONE);
textShape.getLine().setFillType(FillFormatType.NONE);
//append a new slide
presentation.getSlides().append();
//save to file
presentation.saveToFile("output/UniqueSlideMaster.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}
Output
Sample 2 Apply Multiple Slide Maters in PowerPoint
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 MultiSlideMasters {
public static void main(String[] args)throws Exception {
//create a Presentation object and specify the slide size
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//add four new slides to the presentation
for (int i = 0; i < 4; i++)
{
presentation.getSlides().append();
}
//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\\Test1\\Desktop\\Image1.jpg";
String pic2 = "C:\\Users\\Test1\\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 of 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("output/ApplyMultiMasters.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}
Output
No comments:
Post a Comment