In a PowerPoint document, we can set the transparency of the pictures in the shapes to enable the text in the shapes to be displayed on top of the picture without being obscured by it. In this article, I’ll introduce how to set transparency for images in PowerPoint using Java codes.
DEPENDENCY
We need to use a free third-party library called Free Spire.Presentation for Java in order to realize the
function above. First of all, we add the Spire.Presentation.jar file as a dependency in Java program. It can
be gotten from the link, or we can directly import it by adding the following code to the 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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
USING THE CODE
The code sample will insert an image to a specified slide in PowerPoint, and then set its transparency
in order to show the text in the shape above the image. The following are some steps to do that.
l Create a Presentation instance and load a PowerPoint sample document using Presentation.loadFromFile()
method.
l 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.
l Fill the shape with an image and set the fill format type using IAutoShape.getFill().setFillType(FillFormatType.value) method.
l Get the picture fill format using IAutoShape.getFill().getPictureFill() method.
l 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.
l Save the document using Presentation.saveToFile() method.
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(220, 65, 260, 70);
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("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);
}
}
Output
No comments:
Post a Comment