A text box refers to a movable and resizable text or graphic container. In PowerPoint, if you need to add new content, you often need to insert a new text box. In this article, how to use Free Spire.Presentation for Java to add a textbox to a PowerPoint slide and set the textbox border style, fill effect, shadow effect, textbox rotation, textstyle and more.
** Import JAR package ** ** Method 1: ** Download Free Spire.Presentation for Java, unzip it, and in the lib folder Import the Spire.Presentation.jar package into your Java application as a dependency. ** Method 2: ** Install the JAR package directly from the Maven repository and configure the pom.xml file as follows:
<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>2.6.1</version>
</dependency>
</dependencies>
** Java code **
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.GradientShapeType;
import com.spire.presentation.drawing.OuterShadowEffect;
import java.awt.*;
public class AddTextBox {
public static void main(String[]args)throws Exception {
//Create document
Presentation ppt = new Presentation();
//Takes the first slide and adds a rectangular textbox of the specified size and position
IAutoShape tb = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle(80, 120, 550, 200));
//Set the border style of the text box
tb.getLine().setFillType(FillFormatType.SOLID);
tb.getLine().setWidth(2.5);
tb.getLine().getSolidFillColor().setColor(Color.white);
//Add text to the text box and format the text
tb.appendTextFrame("Thanks for watching!\n Thanks for Watching");
PortionEx textRange = tb.getTextFrame().getTextRange();
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(Color.white);
textRange.setFontHeight(30);
textRange.setLatinFont(new TextFont("Arial Unicode MS"));
//Fill the text box with a gradient color
tb.getFill().setFillType(FillFormatType.GRADIENT);
tb.getFill().getGradient().setGradientShape(GradientShapeType.LINEAR);
tb.getFill().getGradient().getGradientStops().append(1f,KnownColors.LIGHT_SEA_GREEN);
tb.getFill().getGradient().getGradientStops().append(0f,KnownColors.LIGHT_PINK);
//Set the shadow effect of the text box
OuterShadowEffect shadowEffect= new OuterShadowEffect();
shadowEffect.setBlurRadius(20);
shadowEffect.setDirection(30);
shadowEffect.setDistance(8);
shadowEffect.getColorFormat().setColor(Color.LIGHT_GRAY);
tb.getEffectDag().setOuterShadowEffect(shadowEffect);
//Set the text box to rotate 5 degrees to the right (rotate to the left sets the value to a negative number)
tb.setRotation(5);
//Save the document
ppt.saveToFile("addTextBox.pptx",FileFormat.PPTX_2013);
ppt.dispose();
}
}
** Result of adding text box: **
Recommended Posts