Java: Set Transparency for Images in PowerPoint

When an image appears over the text, the text will be covered by the image and will be unable to be displayed. At this point, you can set the transparency of the picture in order to show the text above the image. This article is to demonstrate how to insert an image into a specific slide position in PowerPoint and set its transparency 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.

  • Package Manager
<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 Transparency for Images in PowerPoint

A number of classes and methods are involved in this code example, so a table shown as below is provided to make it easier for you to learn about them.

Name Description
IAutoShape Interface Represents a shape.
ShapeList Class Represents a collection of shapes.
PictureFillFormat Class Represents a picture fill style.
ShapeList.appendShape(ShapeType.shapeType, Rectangle2D.Double) Method Adds a new shape to list.
IAutoShape.getLine().setFillType(FillFormatType.value)  Method Sets the fill format type of the line.
IAutoShape.getFill().setFillType(FillFormatType.value) Method Sets the fill format type of a shape.
IAutoShape.getFill().getPictureFill() Method Gets the picture fill format.
PictureFillFormat.setFillType(PictureFillType.value) Method Sets the picture fill mode.
PictureFillFormat.getPicture().setUrl(java.lang.String value) Method Sets the image's URL for a picture fill.
PictureFillFormat.getPicture().setTransparency() Method Sets the transparency of a picture fill.

The following are some steps to set transparency for images in PowerPoint:

  • Create a Presentation instance and load a PowerPoint sample document using Presentation.loadFromFile() method.
  • Get a specified slide using Presentation.getSlides().get() method, and insert a shape to the specified position of the slide using ShapeList.appendShape(ShapeType.shapeType, Rectangle2D.Double) method.
  • Fill the shape with an image and set the fill format type using IAutoShape.getFill().setFillType(FillFormatType.value) method.
  • Get the picture fill format using IAutoShape.getFill().getPictureFill() method.
  • Set the picture fill mode using PictureFillFormat.setFillType(PictureFillType.value) method, set the image’s URL using PictureFillFormat.getPicture().setUrl(java.lang.String value) method, and set the transparency of the image using PictureFillFormat.getPicture().setTransparency() method.
  • Save the document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.geom.Rectangle2D;

public class SetImageTransparency {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation presentation = new Presentation();

        //Load a PowerPoint sample document
        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pptx");

        //Insert a shape to the specified position of the first slide
        Rectangle2D.Double rect1 = new   Rectangle2D.Double(50, 130, 275, 150);
        IAutoShape shape =  presentation.getSlides().get(1).getShapes().appendShape(ShapeType.RECTANGLE, rect1);

        //Fill the shape with an image
        shape.getLine().setFillType(FillFormatType.NONE);//Sets the fill format type
        shape.getFill().setFillType(FillFormatType.PICTURE);//Sets the type of filling
        shape.getFill().getPictureFill().getPicture().setUrl("https://cdn.e-iceblue.com/C:\\Users\\Test1\\Desktop\\Picture.png");//Sets the linked image's URL
        shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);//Sets the picture fill mode

        //Set transparency of the image
        shape.getFill().getPictureFill().getPicture().setTransparency(50);

        //Save the document to file
        presentation.saveToFile("output/SetImageTransparency_result.pptx", FileFormat.PPTX_2010);
    }
}

Java: Set Transparency for Images 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.