Friday, 2 July 2021

[Java] Set shadow effects for text/shapes/images in PowerPoint slides

In PowerPoint documents, shadows will make your objects pop out of your slide through making flat 2 dimensional objects appear like 3 dimensional objects. In this article, we’ll learn three types of shadow effects in PowerPoint — Outer, Inner and Perspective, and also know how to set these shadow effects for text/shapes/images in PowerPoint slide using Java codes without installing Microsoft PowerPoint.

DEVELOPMENT ENVIRONMENT

We need to use a free third-party library called Free Spire.Presentation for Java to manipulate the operation above in Java applications. Get the product package from the link, find Spire.Presentation.jar in the “lib” folder and then manually add it to the project. Or you can directly reference the Jar file by using the following Maven configurations.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://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>

ABOUT THREE TYPES OF SHADOW EFFECTS

Outer shadow

Outer shadow preset is used to make the object pop out of the screen. Here is a screenshot of text which

is from a PowerPoint slide, and now I’ll use Java codes to set an outer shadow effect for it.

Java codes:

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.OuterShadowEffect;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class SetShadowEffectForText {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

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

//Add a rectangle to the slide
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(80,60,800,200));
shape.getFill().setFillType(FillFormatType.NONE);
shape.getLine().setFillType(FillFormatType.NONE);

//Fill the shape with text
shape.appendTextFrame("SHADOW EFFECTS");

//Set font style
shape.getTextFrame().getTextRange().setFontHeight(36f);
shape.getTextFrame().getTextRange().setLatinFont(new TextFont("Arial Black"));
shape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
shape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);

//Set an outer shadow effect for the text through the OuterShadowEffect method
OuterShadowEffect outerShadow= new OuterShadowEffect();
outerShadow.setBlurRadius(0);
outerShadow.setDirection(50);
outerShadow.setDistance(10);
outerShadow.getColorFormat().setColor(Color.yellow);

//Apply the outer shadow effect to text
shape.getTextFrame().getTextRange().getEffectDag().setOuterShadowEffect(outerShadow);

//Save the resulting file
presentation.saveToFile("output/SetShadowEffectForText.pptx", FileFormat.PPTX_2013);
}
}

The generated text is showed as below after setting an outer shadow effect for it.


Inner shadow

Inner shadow preset makes your shape look like it is pressed inside from the surface. Use the following

Java codes to set an inner shadow effect for a shape in a PowerPoint slide.

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

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

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

//Add a rectangle to the slide
Rectangle2D rect1 = new Rectangle2D.Float(200, 150, 300, 200);
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect1);
shape.getFill().setFillType(FillFormatType.SOLID);
shape.getFill().getSolidColor().setColor(Color.white);
shape.getLine().setFillType(FillFormatType.NONE);

//Set an inner shadow effect for the shape through the InnerShadowEffect method
InnerShadowEffect innerShadow = new InnerShadowEffect();
innerShadow.setBlurRadius(20);
innerShadow.setDirection(0);
innerShadow.setDistance(0);
innerShadow.getColorFormat().setColor(Color.BLACK);

//Apply the inner shadow effect to the shape
shape.getEffectDag().setInnerShadowEffect(innerShadow);

//save the resulting file
ppt.saveToFile("output/setShadowEffectForShape.pptx", FileFormat.PPTX_2013);
}
}
A comparison between two shapes is showed as below.

Perspective Shadow

Perspective shadow makes your 2D shapes look like 3D shapes. I’ll set the shadow effect for an image

in a PowerPoint slide using the Java codes below.

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

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

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

//Add a rectangle to the slide.
Rectangle2D rect = new Rectangle2D.Float(120, 100, 250, 270);
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);

//Fill the shape with a picture
shape.getFill().setFillType(FillFormatType.PICTURE);
shape.getFill().getPictureFill().getPicture().setUrl("C:\\Users\\Test1\\Desktop\\logo.png");
shape.getLine().setFillType(FillFormatType.NONE);
shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);

//Set a perspective shadow effect for the image through the PresetShadowEffect method
PresetShadow presetShadow = new PresetShadow();
presetShadow.setPreset(PresetShadowValue.BACK_RIGHT_PERSPECTIVE);
presetShadow.getColorFormat().setColor(Color.lightGray);

//Apply the perspective shadow effect to the image
shape.getEffectDag().setPresetShadowEffect(presetShadow);

//Save the resulting file
ppt.saveToFile("output/SetShadowEffectForImage.pptx", FileFormat.PPTX_2013);
}
}

From the comparison image, it feels as if the dolphin is standing vertically on the floor.



No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...