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.3.1</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); } }
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.
Save PowerPoint Shapes as Images in Java
This article shows you how to export shapes in a specific slide as images using Spire.Presentation for Java. Below is a screenshot of the sample PowerPoint document.
import com.spire.presentation.ISlide; import com.spire.presentation.Presentation; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; public class SaveShapeAsImage { public static void main(String[] args) throws Exception { //Create a Presentation object Presentation presentation = new Presentation(); //Load the sample PowerPoint file presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\chart and table.pptx"); //Get the first slide ISlide slide = presentation.getSlides().get(0); //Declare a BufferedImage variable BufferedImage image; //Loop through the shapes in the slide for (int i = 0; i < slide.getShapes().getCount(); i++) { //Save the specific shape as image data image = slide.getShapes().saveAsImage(i); //Write data to png file File file = new File(String.format("ToImage-%d.png", i)); ImageIO.write(image, "PNG", file); } } }
Output
Add Shadow Effects to Shapes in PowerPoint in Java
This article demonstrates how to add shadow effects to shapes in PowerPoint by using Spire.Presentation for Java. In addition to the PresetShadowEffect shown in this article, you can also add InnerShadowEffect, OuterShadowEffect and SoftEdgeEffect, etc.
import com.spire.presentation.*; import com.spire.presentation.drawing.*; import java.awt.*; import java.awt.geom.Rectangle2D; public class setShadowEffect { 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); //Add a shape to the slide. Rectangle2D rect = new Rectangle2D.Float(120, 100, 150, 300); IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect); //Fill the shape with a picture shape.getFill().setFillType(FillFormatType.PICTURE); shape.getFill().getPictureFill().getPicture().setUrl("https://cdn.e-iceblue.com/C:\\Users\\Administrator\\Desktop\\img.png"); shape.getLine().setFillType(FillFormatType.NONE); shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH); //Set shadow effect PresetShadow presetShadow = new PresetShadow(); presetShadow.setPreset(PresetShadowValue.BACK_RIGHT_PERSPECTIVE); presetShadow.getColorFormat().setColor(Color.lightGray); //Apply the shadow effect to shape shape.getEffectDag().setPresetShadowEffect(presetShadow); //Save the document ppt.saveToFile("output/ShapeShadow.pptx", FileFormat.PPTX_2013); } }
Remove Text Box in PowerPoint in Java
This article demonstrates how to remove text box in a PowerPoint document by using Spire.Presentation for Java.
Below is a screenshot of the original PowerPoint document:
import com.spire.presentation.*; public class removeTextBox { public static void main(String[] args) throws Exception { //Load the sample document Presentation ppt = new Presentation(); ppt.loadFromFile("sample.pptx"); //Get the first slide ISlide slide = ppt.getSlides().get(0); //Traverse all the shapes in the slide and remove the textboxes for (int i = slide.getShapes().getCount() - 1; i >= 0; i--) { IAutoShape shape = (IAutoShape) slide.getShapes().get(i); if (shape.isTextBox()) { slide.getShapes().removeAt(i); } } //Save the document ppt.saveToFile("output/removeTextBox.pptx", FileFormat.PPTX_2013); } }
Output
How to Repeat an Animation in PowerPoint in Java
By default, an animation plays only one time and does not repeat. However, we can make the animation to play more than once by setting the repeat type of it. This article demonstrates how to accomplish this function using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.animation.AnimationEffect; public class RepeatAnimation { public static void main(String[] args) throws Exception { //Create a Presentation instance Presentation ppt = new Presentation(); //Load a PowerPoint document ppt.loadFromFile("Animation.pptx"); //Get the first slide ISlide slide = ppt.getSlides().get(0); //Get the first animation effect on the slide AnimationEffect animation = slide.getTimeline().getMainSequence().get(0); //Set the animation effect to repeat forever until the end of slide. animation.getTiming().setAnimationRepeatType(AnimationRepeatType.UtilEndOfSlide); //Save the result document ppt.saveToFile("RepeatAnimation.pptx", FileFormat.PPTX_2013); } }
Output:
Group Shapes in PowerPoint in Java
This article demonstrates how to group shapes in a PowerPoint document using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.geom.Rectangle2D; import java.util.ArrayList; public class GroupShapes { public static void main(String[] args) throws Exception { //create a PowerPoint document Presentation ppt = new Presentation(); //get the first slide ISlide slide = ppt.getSlides().get(0); //add a rectangle shape to the slide IShape rectangle = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(20,100,200,40)); rectangle.getFill().setFillType(FillFormatType.SOLID); rectangle.getFill().getSolidColor().setKnownColor(KnownColors.GOLD); rectangle.getLine().setWidth(0.1f); //add a ribbon shape to the slide IShape ribbon = slide.getShapes().appendShape(ShapeType.RIBBON_2, new Rectangle2D.Double(60, 75, 120, 80)); ribbon.getFill().setFillType(FillFormatType.SOLID); ribbon.getFill().getSolidColor().setKnownColor(KnownColors.PURPLE); ribbon.getLine().setWidth(0.1f); //add the shapes to a list ArrayList list = new ArrayList(); list.add((Shape)rectangle); list.add((Shape)ribbon); //group the shapes ppt.getSlides().get(0).groupShapes(list); //save the resultant document ppt.saveToFile("GroupShapes.pptx", FileFormat.PPTX_2013); } }
Set and Get Alternative Text (Alt Text) of PowerPoint Shapes in Java
Alternative text (Alt Text) can help people with vision or cognitive impairments understand shapes, pictures or other graphical content. This article demonstrates how to set and get the alternative text of a shape in a PowerPoint document using Spire.Presentation for Java.
Set alternative text
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.*; import java.awt.geom.Rectangle2D; public class SetAltText { public static void main(String[] args) throws Exception { //instantiate a Presentation object Presentation ppt = new Presentation();throws Exception //add a shape to the first slide IAutoShape shape = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(115, 130, 100, 100)); shape.getFill().setFillType(FillFormatType.SOLID); shape.getFill().getSolidColor().setColor(Color.orange); shape.getShapeStyle().getLineColor().setColor(Color.white); //set alt text (title and description) for the shape shape.setAlternativeTitle("Triangle"); shape.setAlternativeText("This is a triangle."); //save the resultant document ppt.saveToFile("Output.pptx", FileFormat.PPTX_2013); } }
Get alternative text
import com.spire.presentation.*; public class GetAltText { public static void main(String[] args) throws Exception { //load PowerPoint document Presentation ppt = new Presentation(); ppt.loadFromFile("Output.pptx"); //get the first shape in the first slide IShape shape = ppt.getSlides().get(0).getShapes().get(0); //get the alt text (title and description) of the shape String altTitle = shape.getAlternativeTitle(); String altDescription = shape.getAlternativeText(); System.out.println("Title: " + altTitle); System.out.println("Description: " + altDescription); } }
Replace an Image in PowerPoint in Java
This article demonstrates how to replace an existing image in a PowerPoint document with a new image by using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.IImageData; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.FileInputStream; public class ReplaceImage { public static void main(String[] args) throws Exception { //create a Presentation object Presentation presentation= new Presentation(); //load the sample PowerPoint file presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx"); //add an image to the image collection String imagePath = "C:\\Users\\Administrator\\Desktop\\Microsoft-PowerPoint-logo.jpg"; BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imagePath)); IImageData image = presentation.getImages().append(bufferedImage); //get the shape collection from the first slide ShapeCollection shapes = presentation.getSlides().get(0).getShapes(); //loop through the shape collection for (int i = 0; i < shapes.getCount(); i++) { //determine if a shape is a picture if (shapes.get(i) instanceof SlidePicture) { //fill the shape with a new image ((SlidePicture) shapes.get(i)).getPictureFill().getPicture().setEmbedImage(image); } } //save to file presentation.saveToFile("output/ReplaceImage.pptx", FileFormat.PPTX_2013); } }
Save Chart, Table and Shape on Presentation slides as Image in Java
Spire.Presentation for Java supports to save the whole presentation slides with table, chart and shape to image. And it also supports to save the single element in the presentation slides, such as chart, table and shape into image. This article will show you how to save shape and chart on Presentation Slide as image in Java.
import com.spire.presentation.ISlide; import com.spire.presentation.Presentation; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class ShapeAsImage { public static void main(String[] args) throws Exception { String inputFile = "Sample.pptx"; String outputFile = "output/"; //Create a Presentation instance and load sample file Presentation presentation = new Presentation(); presentation.loadFromFile(inputFile); //Traverse every slides and every shapes on the slides int k = 0; for (int i = 0; i < presentation.getSlides().getCount(); i++) { ISlide slide = presentation.getSlides().get(i); for (int j = 0; j < slide.getShapes().getCount(); j++) { String fileName = outputFile + String.format("shapeToImage-%1$s.png", k); //Save every single shape as image BufferedImage image = slide.getShapes().saveAsImage(j); ImageIO.write(image, "PNG", new File(fileName)); k++; } } } }
Add Animations to Shapes in PowerPoint in Java
Animation can make a PowerPoint document more dynamic. In this article, we'll introduce how to add animations to shapes in a PowerPoint document using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import com.spire.presentation.drawing.animation.AnimationEffectType; import java.awt.*; import java.awt.geom.Rectangle2D; public class SetAnimation { public static void main(String[] args) throws Exception { //create a PowrePoint document Presentation ppt = new Presentation(); //add a slide ISlide slide = ppt.getSlides().get(0); //Add a shape to slide IAutoShape shape = slide.getShapes().appendShape(ShapeType.CUBE, new Rectangle2D.Double(50, 150, 150, 150)); shape.getFill().setFillType(FillFormatType.SOLID); shape.getFill().getSolidColor().setColor(Color.orange); shape.getShapeStyle().getLineColor().setColor(Color.white); //Add animation to the shape slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.FADED_SWIVEL); //save the document ppt.saveToFile("AddAnimationToShape.pptx", FileFormat.PPTX_2013); } }