When working with PowerPoint documents, there are times when you may need to add an image on top of the specified text. To avoid the text being covered by the image, you can set the transparency of the image to ensure that the text is displayed over the image. In this article, you will learn how to programmatically insert an image in a specific position in a PowerPoint slide and set the image transparency using a free Java API.
Import Dependency
The free API used to implement this functionality in Java applicaitons is the Free Spire.Presentation for Java.Below are two methods to install it.
● Download the free library and unzip it, and then add the Spire.Presentation.jar file to your project as dependency.
● Directly add the jar dependency to your maven project by adding the following configurations to the pom.xml.
<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.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
Sample Code
To set the transparancy for images in PowerPoint, Free Spire.Presentation for Java provides the PictureFillFormat.getPicture().setTransparency() method. The complete sample code is shown as below.
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("input.pptx");
//Insert a shape to the specified position of the first slide
Rectangle2D.Double rect1 = new Rectangle2D.Double(20, 100, 320, 240);
IAutoShape shape = presentation.getSlides().get(0).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("intro.jpg");//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("SetImageTransparency.pptx", FileFormat.PPTX_2013);
}
}
Top comments (0)