The background of a PowerPoint presentation sets the tone and mood of the presentation and can greatly enhance the aesthetic and impact of the slides. There are five types of backgrounds available in PowerPoint presentations, solid color backgrounds, gradient backgrounds, picture backgrounds, texture backgrounds, and patterned backgrounds. They each apply to different usage scenarios. For example, a professional business presentation may benefit from a clean and simple solid color background, while a creative presentation may use inspiring and interesting picture backgrounds to capture the audience's attention. This article is going to show how to set backgrounds for PowerPoint presentations through Java programs using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.4.5</version>
    </dependency>
</dependencies>
    

Set a Solid Color Background for a PowerPoint Presentation

Before customizing the background, it is necessary to use the SlideBackground.setType(BackgroundType.CUSTOM) method to allow the customization of the background. Then, the background type can be set to solid color background using the SlideBackground.getFill().setFillType(FillFormatType.SOLID) method, and the color can be set using the FillFormat.getSolidColor().setColor() method.

The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to solid color using SlideBackground.getFill().setFillType(FillFormatType.SOLID) method.
  • Customize the background color using FillFormat.getSolidColor().setColor() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormat;
import com.spire.presentation.drawing.FillFormatType;
import org.w3c.dom.css.RGBColor;

import java.awt.*;

public class SolidColor {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to solid color
        background.getFill().setFillType(FillFormatType.SOLID);

        //Set the background color
        FillFormat fillFormat = background.getFill();
        fillFormat.getSolidColor().setColor(new Color(199, 213, 237));

        //Save the presentation
        ppt.saveToFile("SolidColorBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Set a Gradient Background for a PowerPoint Presentation

Gradient background can be set by setting the background type to Gradient Background and then setting the gradient type, color, and angle. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to gradient using SlideBackground.getFill().setFillType(FillFormatType.GRADIENT) method.
  • Set the gradient type to linear gradient using GradientFillFormat.setGradientShape(GradientShapeType.LINEAR) method.
  • Add the gradient stops and set the gradient colors using GradientFillFormat.getGradientStops().append() method.
  • Set the angle of the linear gradient using GradientFillFormat.getLinearGradientFill().setAngle() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.*;

import java.awt.*;

public class Gradient {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to gradient color
        background.getFill().setFillType(FillFormatType.GRADIENT);

        //Set the gradient type to linear
        GradientFillFormat gradient = background.getFill().getGradient();
        gradient.setGradientShape(GradientShapeType.LINEAR);

        //Add gradient stops and set the colors
        gradient.getGradientStops().append(0f, new Color(230, 255, 255));
        gradient.getGradientStops().append(0.5f, new Color(255, 255, 255));
        gradient.getGradientStops().append(1f, new Color(199, 213, 237));

        //Set the angle of the linear gradient
        gradient.getLinearGradientFill().setAngle(90);

        //Save the presentation
        ppt.saveToFile("GradientBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Set a Picture Background for a PowerPoint Presentation

To set the picture background, set the background type to picture, set the picture fill type to stretch fill, and then set the background image. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Load a picture using Presentation.getImages().append() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to picture using SlideBackground.getFill().setFillType(FillFormatType.PICTURE) method.
  • Set the picture fill type to stretch fill using PictureFillFormat.setFillType(PictureFillType.STRETCH) method.
  • Set the transparency of the background using PictureFillFormat.getPicture().setTransparency() method.
  • Set the background image using PictureFillFormat.getPicture().setEmbedImage() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;

public class Picture {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Load a picture
        IImageData image = ppt.getImages().append(ImageIO.read(new File("background.jpg")));

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to picture
        background.getFill().setFillType(FillFormatType.PICTURE);

        //Set the picture fill type to stretch
        PictureFillFormat pictureFillFormat = background.getFill().getPictureFill();
        pictureFillFormat.setFillType(PictureFillType.STRETCH);

        //Set the transparency of the background
        pictureFillFormat.getPicture().setTransparency(50);

        //Set the background picture
        pictureFillFormat.getPicture().setEmbedImage(image);

        //Save the presentation
        ppt.saveToFile("PictureBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Set a Texture Background for a PowerPoint Presentation

Setting a texture background is similar to setting a picture background. The difference is that the image fill type needs to be changed to a tiled fill and the texture alignment needs to be set. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Load the texture using Presentation.getImages().append() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to picture using SlideBackground.getFill().setFillType(FillFormatType.PICTURE) method.
  • Set the picture fill type to tile fill using PictureFillFormat.setFillType(PictureFillType.TILE) method.
  • Set the transparency of the background using PictureFillFormat.getPicture().setTransparency() method.
  • Set the background texture using PictureFillFormat.getPicture().setEmbedImage() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;

import javax.imageio.ImageIO;
import java.io.File;

public class Texture {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Load the texture
        IImageData image = ppt.getImages().append(ImageIO.read(new File("texture.png")));

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to picture
        background.getFill().setFillType(FillFormatType.PICTURE);

        //Set the picture fill type to tile
        PictureFillFormat pictureFillFormat = background.getFill().getPictureFill();
        pictureFillFormat.setFillType(PictureFillType.TILE);

        //Set the texture alignment
        pictureFillFormat.setAlignment(RectangleAlignment.TOP_LEFT);

        //Set the transparency of the background
        pictureFillFormat.getPicture().setTransparency(50);

        //Set the background texture
        pictureFillFormat.getPicture().setEmbedImage(image);

        //Save the presentation
        ppt.saveToFile("TextureBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Set a Pattern Background for a PowerPoint Presentation

In adding a pattern background, it is necessary to set the type of pattern as well as the foreground and background colors of the pattern. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to pattern using SlideBackground.getFill().setFillType(FillFormatType.PATTERN) method.
  • Set the pattern type using PatternFillFormat.setPatternType() method.
  • Set the foreground color of the pattern using PatternFillFormat.getForegroundColor().setColor() method.
  • Set the background color of the pattern using PatternFillFormat.getBackgroundColor().setColor() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;

public class Pattern {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to pattern
        background.getFill().setFillType(FillFormatType.PATTERN);

        //Set the pattern type
        PatternFillFormat patternFillFormat = background.getFill().getPattern();
        patternFillFormat.setPatternType(PatternFillType.DOTTED_GRID);

        //Set the foreground color of the pattern
        patternFillFormat.getForegroundColor().setColor(new Color(230, 255, 255));

        //Set the background color of the pattern
        patternFillFormat.getBackgroundColor().setColor(new Color(199, 213, 237));

        //Save the presentation
        ppt.saveToFile("PatternBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Document Operation
Thursday, 03 August 2023 08:50

Java: Change the Slide Size in PowerPoint

The slide size is one of the important aspects of the visual design of PowerPoint presentations. It influences the aspect ratio and dimensions of the presentation and can have a significant impact on the overall appearance and atmosphere of the presentation. If the default slide size does not meet the visual design requirements or does not match the size of the screen showing the presentation, it is necessary to change the size of the slides to another preset size or customize a slide size. This article will demonstrate how to change the slide size of PowerPoint presentations through Java programs using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.4.5</version>
    </dependency>
</dependencies>
    

Change the Slide Size to a Preset Size

Spire.Presentation for Java provides the Presentation.getSlideSize().setType() method to change the slide size to a preset size. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Change the slide type of the presentation using Presentation.getSlideSize().setType() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;

public class changeSlideSizePreset {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation pt = new Presentation();

        //Load a presentation file
        pt.loadFromFile("Sample.pptx");

        //Change the slide size of the presentation to A4
        pt.getSlideSize().setType(SlideSizeType.A4);

        //Save the presentation file
        pt.saveToFile("A4Size.pptx", FileFormat.AUTO);
        pt.dispose();
    }
}

Java: Change the Slide Size in PowerPoint

Change the Slide Size to a Custom Size

Customizing the size of slides requires changing the slide size type to custom. After that, the Presentation.getSlideSize().setSize() method can be used to customize the slide size. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Change the slide size type to custom using Presentation.getSlideSize().setType() method.
  • Customize the slide size using Presentation.getSlideSize().setSize() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSize;
import com.spire.presentation.SlideSizeType;

import java.awt.*;

public class changeSlideSizeCustom {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation pt = new Presentation();

        //Load a PowerPoint presentation
        pt.loadFromFile("Sample.pptx");

        //Change the slide size type to custom
        pt.getSlideSize().setType(SlideSizeType.CUSTOM);

        //Set the slide size
        pt.getSlideSize().setSize(new Dimension(600, 600));

        //Save the presentation file
        pt.saveToFile("CustomSize.pptx", FileFormat.AUTO);
        pt.dispose();
    }
}

Java: Change the Slide Size in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Document Operation

Suppose there are two PowerPoint documents, and you want to copy a certain slide from one document to a specified location of the other. Manual copying and pasting is an option, but the quicker and more efficient way is to use Java codes for automatic operation. This article will show you how to programmatically copy slides between two different PowerPoint documents using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.4.5</version>
    </dependency>
</dependencies>
    

Copy Slides Between Two PowerPoint Documents

The following are detailed steps to copy a slide from one PowerPoint document to a specified position or the end of the other document.

  • Create a Presentation object and load one sample document using Presentation.loadFromFile() method.
  • Create another Presentation object and load the other sample document using Presentation.loadFromFile() method.
  • Get a specific slide of document one using Presentation.getSlides().get() method and insert its copy into the specified position of document two using Presentation.getSlides().insert() method.
  • Get another specific slide of document one using Presentation.getSlides().get() method and add its copy to the end of document two using Presentation.getSlides().append() method.
  • Save the document two to another file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class CopySlidesBetweenPPT {
    public static void main(String[] args) throws Exception {
        //Create a Presentation object to load one sample document
        Presentation pptOne= new Presentation();
        pptOne.loadFromFile("C:\\Users\\Test1\\Desktop\\sample1.pptx");

        //Create another Presentation object to load the other sample document
        Presentation pptTwo = new Presentation();
        pptTwo.loadFromFile("C:\\Users\\Test1\\Desktop\\sample2.pptx");

        //Insert the specific slide from document one into the specified position of document two
        pptTwo.getSlides().insert(0,pptOne.getSlides().get(0));

        //Append the specific slide of document one to the end of document two
        pptTwo.getSlides().append(pptOne.getSlides().get(3));

        //Save the document two to another file
        pptTwo.saveToFile("output/CopySlidesBetweenPPT.pptx", FileFormat.PPTX_2013);
    }
}

Java: Copy Slides Between Two PowerPoint Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Document Operation

This article demonstrates how to set and get slide title in a PowerPoint document using Spire.Presentation for Java.

Set slide title

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;

public class SetSlideTitle {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);
        
        //Set title for the slide
        slide.setTitle("Tile Text");
        
        //Save the result document
        ppt.saveToFile("SetTitle.pptx", FileFormat.PPTX_2013);
    }
}

Set and Get Slide Title in PowerPoint in Java

Get slide title

import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;

public class GetSlideTitle {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load a PowerPoint document
        ppt.loadFromFile("SetTitle.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Print out the title of the slide
        String tile = slide.getTitle();
        System.out.println(tile);
    }
}

Set and Get Slide Title in PowerPoint in Java

Published in Document Operation

Document properties of PowerPoint presentations hold immense value in managing and organizing presentations. The information in properties, including title, author, keywords, etc., provides a concise summary, aids in categorization and searchability, and contributes to maintaining a comprehensive presentation history. However, some useless document properties need to be deleted to prevent them from affecting document management. This article is going to show how to add, retrieve, or delete document properties in PowerPoint presentations using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.4.5</version>
    </dependency>
</dependencies>
    

Add Document Properties to a PowerPoint Presentation

Spire.Presentation for Java provides a set of methods under IDocumentProperty class to enable users to set and retrieve document properties of a presentation after getting the properties using the Presentation.getDocumentProperty() method. The detailed steps for adding document properties to a PowerPoint presentation are as follows:

  • Create an object of Presentation class.
  • Load a presentation file using Presentation.loadFromFile() method.
  • Get the document properties of the presentation using Presentation.getDocumentProperty() method.
  • Set the document properties using methods under IDocumentProperty class.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.IDocumentProperty;
import com.spire.presentation.Presentation;

public class addPresentationProperties {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation presentation = new Presentation();

        //Load a PowerPoint presentation
        presentation.loadFromFile("Sample.pptx");

        //Get the properties of the presentation and add items
        IDocumentProperty property = presentation.getDocumentProperty();
        property.setTitle("Annual Business Analysis Report");
        property.setSubject("Business Analysis");
        property.setAuthor("Taylor");
        property.setManager("John");
        property.setCompany("E-iceblue");
        property.setCategory("Report");
        property.setKeywords("Operating Analysis; Quarterly Operating Data; Growth");
        property.setComments("The report has been revised and finalized and does not require further consultation.");

        //Save the presentation file
        presentation.saveToFile("AddProperties.pptx", FileFormat.AUTO);
        presentation.dispose();
    }
}

Java: Set, Retrieve, or Delete Document Properties in PowerPoint

Retrieve Document Properties from a PowerPoint Presentation

The detailed steps for retrieving document properties from a PowerPoint presentation are as follows:

  • Create an object of Presentation class.
  • Load a presentation file using Presentation.loadFromFile() method.
  • Get the properties of the presentation using Presentation.getDocumentProperty() method.
  • Retrieve property information using methods under IDocumentProperty class and write it to a text file.
  • Java
import com.spire.presentation.IDocumentProperty;
import com.spire.presentation.Presentation;

import java.io.FileWriter;

public class retrievePresentationProperties {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation presentation = new Presentation();

        //Load a presentation file
        presentation.loadFromFile("AddProperties.pptx");

        //Get the properties of the presentation
        IDocumentProperty property = presentation.getDocumentProperty();

        //Get the properties and write them to a text file
        String properties = "Title: " + property.getTitle() + "\r\n"
                + "Subject: " + property.getSubject() + "\r\n"
                + "Author: " + property.getAuthor() + "\r\n"
                + "Manager: " + property.getManager() + "\r\n"
                + "Company: " + property.getCompany() + "\r\n"
                + "Category: " + property.getCategory() + "\r\n"
                + "Keywords: " + property.getKeywords() + "\r\n"
                + "Comments: " + property.getComments();
        FileWriter presentationProperties = new FileWriter("PresentationProperties.txt");
        presentationProperties.write(properties);
        presentationProperties.close();
    }
}

Java: Set, Retrieve, or Delete Document Properties in PowerPoint

Delete Document Properties of a PowerPoint Presentation

Removing document properties from a presentation is similar to adding properties. Just set the property data to null and the document properties can be removed. The details are as follows:

  • Create an object of Presentation class.
  • Load a presentation file using Presentation.loadFromFile() method.
  • Get the document properties of the presentation using Presentation.getDocumentProperty() method.
  • Set the document properties to null using methods under IDocumentProperty class.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.IDocumentProperty;
import com.spire.presentation.Presentation;

public class deletePresentationProperties {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation presentation = new Presentation();

        //Load a PowerPoint presentation
        presentation.loadFromFile("AddProperties.pptx");

        //Get the properties of the presentation and set the properties to null
        IDocumentProperty property = presentation.getDocumentProperty();
        property.setTitle("");
        property.setSubject("");
        property.setAuthor("");
        property.setManager("");
        property.setCompany("");
        property.setCategory("");
        property.setKeywords("");
        property.setComments("");

        //Save the presentation file
        presentation.saveToFile("DeleteProperties.pptx", FileFormat.AUTO);
        presentation.dispose();
    }
}

Java: Set, Retrieve, or Delete Document Properties in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Document Operation

This article will demonstrate how to set the layout of the slide via Spire.Presentation in Java applications. There are 11 kinds of layout on Microsoft PowerPoint and Spire.Presentation supports all of them.

How to set the layout of the slide in Java

Set one layout of the slide

import com.spire.presentation.*;

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

        //Create an instance of presentation document
        Presentation ppt = new Presentation();

        //Remove the default slide
        ppt.getSlides().removeAt(0);

        //Append a slide and set the layout for slide
        ISlide slide = ppt.getSlides().append(SlideLayoutType.TITLE);

        //Add content for Title and Text
        IAutoShape shape = (IAutoShape)slide.getShapes().get(0);
        shape.getTextFrame().setText("Spire.Presentation");

        shape = (IAutoShape)slide.getShapes().get(1);
        shape.getTextFrame().setText("Set the Layout of Slide as Title");

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

How to set the layout of the slide in Java

Set the different layout of the slides

import com.spire.presentation.*;

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

        //Create a PPT document
        Presentation presentation = new Presentation();

        //Remove the default slide
        presentation.getSlides().removeAt(0);

        //Loop through slide layouts
        for (SlideLayoutType type : SlideLayoutType.values())
        {
            //Append slide by specified slide layout
            presentation.getSlides().append(type);
        }

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

Effective screenshot:

How to set the layout of the slide in Java

Published in Document Operation

This article demonstrates how to split a PowerPoint document into multiple individual slides using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class SplitPowerPoint {

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

        //Load the sample PowerPoint file
        Presentation ppt = new Presentation();
        ppt.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");

        //Loop through the slides
        for (int i = 0; i < ppt.getSlides().getCount(); i++)
        {
            //Create an instance of Presentation class
            Presentation newppt = new Presentation();

            //Remove the default slide
            newppt.getSlides().removeAt(0);

            //Select a slide from the source file and append it to the new document
            newppt.getSlides().append(ppt.getSlides().get(i));

            //Save to file
            newppt.saveToFile(String.format("output/result-%d.pptx",i), FileFormat.PPTX_2013);
        }
    }
}

Output:

Split PowerPoint Document into Individual Slides in Java

Published in Document Operation
Friday, 08 November 2019 01:57

Apply Background Image to Slides in Java

This article demonstrates how to apply background image to all slides in a PowerPoint presentation using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class AppplyBgToAllSlides {

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

        //load a PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the image data
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\bg.jpg"));
        IImageData imageData = presentation.getImages().append(bufferedImage);

        //loop through the slides
        for (int i = 0; i < presentation.getSlides().getCount() ; i++) {

            //apply the image to the specific slide as background
            SlideBackground background = presentation.getSlides().get(i).getSlideBackground();
            background.setType(BackgroundType.CUSTOM);
            background.getFill().setFillType(FillFormatType.PICTURE);
            background.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
            background.getFill().getPictureFill().getPicture().setEmbedImage(imageData);
        }

        //save the file
        presentation.saveToFile("output/BackgroundImage.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Apply Background Image to Slides in Java

Published in Document Operation

The slide master in PowerPoint preserves some fixed styles, such as background image, title and theme color, which can be inherited by other slides. This article demonstrates how to customize the slide masters in a PowerPoint file and apply them to different slides using Spire.Presentation for Java.

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

    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/Administrator/Desktop/bg.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/Administrator/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(40,40,200,60));
        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()-200,(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("Chengdu E-iceblue Co., Ltd.");
        textShape.getTextFrame().getTextRange().setFontHeight(15f);
        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("ModifySlideMaster.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Modify and Apply Slide Maters in PowerPoint in Java

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

    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/Administrator/Desktop/image1.png";
        String pic2 = "C:/Users/Administrator/Desktop/image2.png";
        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();
    }
}

Modify and Apply Slide Maters in PowerPoint in Java

Published in Document Operation

Slides are one of the important elements in a PowerPoint document because they carry the information or ideas you would like to present to your audience. When editing a PowerPoint document, it's inevitable that you will perform operations on presentation slides, such as creating new slides, deleting unwanted slides, or hiding slides temporarily. This article will demonstrate how to programmatically add, hide/unhide or delete a PowerPoint slide using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.4.5</version>
    </dependency>
</dependencies>
    

Add a New Slide at the End of the PowerPoint Document

The Presentation.getSlides().append() method provided by Spire.Presentation for Java allows you to append a new slide after the last slide of a PowerPoint document. The detailed steps are as follows.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Add a new blank slide at the end of the document using Presentation.getSlides().append() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class AddNewSlideinPowerPoint {
    public static void main(String[] args) throws Exception {
        //Initialize an instance of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample PowerPoint document
        presentation.loadFromFile("Sample.pptx");

        //Add a new slide at the end of the document
        presentation.getSlides().append();

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

Java: Add, Hide or Delete Slides in PowerPoint

Insert a New Slide Before a Specific Slide in PowerPoint

Sometimes you may also need to insert a slide before a specific slide to add additional supporting information, and below are the detailed steps to accomplish the task.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Insert a blank slide before a specified slide using Presentation.getSlides().insert() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class InsertSlideinPowerPoint {
    public static void main(String[] args) throws Exception {
        //Initialize an instance of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample PowerPoint document
        presentation.loadFromFile("Sample.pptx");

        //Insert a blank slide before the second slide
        presentation.getSlides().insert(1);

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

Java: Add, Hide or Delete Slides in PowerPoint

Hide or Unhide a Specific Slide in PowerPoint

For the slides that you temporarily do not want to show during a presentation, you can simply hide the slides without deleting them. Below are the steps to hide or unhide a specific PowerPoint slide.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Get a specified slide using Presentation.getSlides().get() method.
  • Hide or unhide the slide by setting the value of ISlide.setHidden() method to true or false.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class HideUnhideSlides {
    public static void main(String[] args) throws Exception {
        //Initialize an instance of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample PowerPoint document
        presentation.loadFromFile("Sample.pptx");

        //Get the third slide
        ISlide slide = presentation.getSlides().get(2);

        //Hide the slide
        slide.setHidden(true);

        //Unhide the slide
        //slide.setHidden(false);

        //Save the result document
        presentation.saveToFile("HideSlide.pptx", FileFormat.PPTX_2010);
    }
}

Java: Add, Hide or Delete Slides in PowerPoint

Delete a Specific Slide from a PowerPoint Document

If you want to remove a unnecessary slide from the document, you can use the Presentation.getSlides().removeAt(int index) method. The detailed steps are as follows.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Remove a specified slide from the document using Presentation.getSlides().removeAt() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class DeletePowerPointSlide {
    public static void main(String[] args) throws Exception {
        //Initialize an instance of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample PowerPoint document
        presentation.loadFromFile("Sample");

        //Remove the first slide
        presentation.getSlides().removeAt(0);

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

Java: Add, Hide or Delete Slides in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Document Operation