Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Adding Text Boxes to PowerPoint Slides with Java

Notes 1

A text box is a movable, resizable container for text and graphics in PowerPoint. When you need to add new content to a presentation, inserting a text box is often the solution. This article demonstrates how to add text boxes to PowerPoint slides using Free Spire.Presentation for Java, along with customization options such as border styles, fill effects, shadows, rotation, and text formatting.

Adding the Dependency

Option 1: Downlaod the Free Spire.Presentation for Java package and extract it. Import the JAR files from the lib folder as dependencies in your Java project.

Option 2: Install via Maven by adding the following configuration to your pom.xml:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>2.6.1</version>
    </dependency>
</dependencies>

Implementation Example

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 InsertTextBox {
    public static void main(String[] args) throws Exception {
        Presentation presentation = new Presentation();

        // Add a rectangular text box with specific dimensions and position
        IAutoShape textBox = presentation.getSlides().get(0).getShapes()
                .appendShape(ShapeType.RECTANGLE, new Rectangle(100, 150, 600, 220));

        // Configure border style
        textBox.getLine().setFillType(FillFormatType.SOLID);
        textBox.getLine().setWidth(3.0);
        textBox.getLine().getSolidFillColor().setColor(Color.WHITE);

        // Insert text and apply formatting
        textBox.appendTextFrame("Welcome!");
        PortionEx textPortion = textBox.getTextFrame().getTextRange();
        textPortion.getFill().setFillType(FillFormatType.SOLID);
        textPortion.getFill().getSolidColor().setColor(Color.WHITE);
        textPortion.setFontHeight(32);
        textPortion.setLatinFont(new TextFont("Arial Unicode MS"));

        // Apply gradient fill to the text box
        textBox.getFill().setFillType(FillFormatType.GRADIENT);
        textBox.getFill().getGradient().setGradientShape(GradientShapeType.LINEAR);
        textBox.getFill().getGradient().getGradientStops().append(0f, KnownColors.TEAL);
        textBox.getFill().getGradient().getGradientStops().append(1f, KnownColors.CORAL);

        // Add shadow effect
        OuterShadowEffect shadow = new OuterShadowEffect();
        shadow.setBlurRadius(15);
        shadow.setDirection(45);
        shadow.setDistance(6);
        shadow.getColorFormat().setColor(Color.DARK_GRAY);
        textBox.getEffectDag().setOuterShadowEffect(shadow);

        // Rotate text box 5 degrees clockwise (negative values rotate counterclockwise)
        textBox.setRotation(5);

        // Save the presentation
        presentation.saveToFile("TextBoxDemo.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

This example creates a new presentation, adds a rectangular text box with a gradient fill, configures the border and text styling, applies an outer shadow effect, and rotates the box slightly before saving the file.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.