[Java] Create and apply a slide master

The PPT Master has powerful template features that allow users to design slide title text, background images, theme colors and more as needed. Once the PPT master design is successful, you can call this template directly and apply it to other slides to avoid repeated edits. This article will show you how to use code in your Java application to create a slide master style and apply it to other slides.

** Import JAR package ** ** Method 1: ** Download Free Spire.Presentation for Java, unzip it, and in the lib folder Import the Spire.Presentation.jar package into your Java application as a dependency. ** Method 2: ** Install the JAR package directly from the Maven repository and configure the pom.xml file as follows:

<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.free</artifactId>
        <version>2.6.1</version>
    </dependency>
</dependencies>

** Create a unique master **

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 CreateSlideMaster {

    public static void main(String[] args) throws Exception {


        //Create a PPT document 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);

        //Get image address
        String backgroundPic = "pic.jpg ";
        String logo = "logo.jpg ";

        //Set the background for the slide master
        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 images to the slide master
        image = ImageIO.read(new FileInputStream(logo));
        imageData = presentation.getImages().append(image);
        IEmbedImage imageShape = masterSlide.getShapes().appendEmbedImage(ShapeType.RECTANGLE,imageData,new Rectangle2D.Float(60,60,220,80));
        imageShape.getLine().setFillType(FillFormatType.NONE);

        //Add text to the slide master
        IAutoShape textShape = masterSlide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float((float) presentation.getSlideSize().getSize().getWidth()-200,(float) presentation.getSlideSize().getSize().getHeight()-50,200,30));//Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(ppt.SlideSize.Size.Width-200, ppt.SlideSize.Size.Height-60, 200, 30));
        textShape.getTextFrame().setText("Work summary report");
        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);

        //Add slide
        presentation.getSlides().append();

        //Save the document
        presentation.saveToFile("output/SlideMaster.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

s1.jpg

** Create multiple masters and apply them individually to slides **

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 CreateMultiSlideMasters {

    public static void main(String[] args) throws Exception {

        //Create a new PPT document
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Insert 4 slides (the document has 5 pages, including the default slides)
        for (int i = 0; i < 4; i++)
        {
            presentation.getSlides().append();
        }

        //Get the default slide master
        IMasterSlide first_master = presentation.getMasters().get(0);

        //Create and get a second slide master
        presentation.getMasters().appendSlide(first_master);
        IMasterSlide second_master = presentation.getMasters().get(1);

        //Set different background images for the two masters
        String pic1 = "image1.jpg ";
        String pic2 = "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 slide master and layout to the first page (plate 6 is empty)
        presentation.getSlides().get(0).setLayout(first_master.getLayouts().get(6));

        //Apply the second slide master and layout to the remaining slides
        for (int i = 1; i < presentation.getSlides().getCount(); i++)
        {
            presentation.getSlides().get(i).setLayout(second_master.getLayouts().get(6));
        }

        //Save the document
        presentation.saveToFile("MultiSlideMaters.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

s2.jpg

Recommended Posts

[Java] Create and apply a slide master
[Java] Create a filter
Create a java method [Memo] [java11]
[Java] Create a temporary file
Create a JAVA WEB application and try OMC APM
Create a Java and JavaScript team development environment (gradle environment construction)
Create a Java project using Eclipse
[Java] How to create a folder
Create a high-performance enum with fields and methods like Java with JavaScript
[Java] Let's create a mod for Minecraft 1.16.1 [Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [9. Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [8. Add and generate ore]
A look at Jenkins, OpenJDK 8 and Java 11
Java implementation to create and solve mazes
Let's create a Java development environment (updating)
Create a TODO app in Java 7 Create Header
A Java engineer compared Swift, Kotlin, and Java.
Install Docker and create Java runtime environment
[Java] Create a jar file with both compressed and uncompressed with the jar command
Create API using Retrofit2, Okhttp3 and Gson (Java)
Create barcodes and QR codes in Java PDF
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
Create a Lambda Container Image based on Java 15
[Java] Create something like a product search API
[Java] [POI] Create a table in Word and start a new line in one cell
Try to create a bulletin board in Java
[Java] Create a collection with only one element
Let's create a super-simple web framework in Java
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
Create Scala Seq from Java, make Scala Seq a Java List
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
Java and JavaScript
XXE and Java
Prepare a scraping environment with Docker and Java
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
I tried to create a shopping site administrator function / screen with Java and Spring
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Create a Java (Maven) project with VS Code and develop it on a Docker container
Create a Java Servlet and JSP WAR file to deploy to Apache Tomcat 9 in Gradle
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
Create a Java development environment using jenv on Mac
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
Create a docker image that runs a simple Java app
How to create a Java environment in just 3 seconds
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[Beginner] Create a competitive game with basic Java knowledge
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
Write a class in Kotlin and call it in Java
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
Java / Twitter clone / task management system (1) Create a database
I tried to create a Clova skill in Java
[Java] Let's create a mod for Minecraft 1.14.4 [1. Add items]
How to create a data URI (base64) in Java
[Note] Create a java environment from scratch with docker
How to convert A to a and a to A using AND and OR in Java