How to Format Text in Java

How to Format Text in Java

Aspose.Slides FOSS for Java provides fine-grained text formatting through the PortionFormat class. A Portion is the smallest independent unit of text; it maps to a single formatting run within a paragraph. This guide shows how to apply bold, italic, font size, and color formatting to text in a presentation.

Step-by-Step Guide

Step 1: Add the Maven Dependency

<dependency>
  <groupId>org.aspose.slides.foss</groupId>
  <artifactId>aspose-slides-foss</artifactId>
  <version>1.0.0</version>
</dependency>

Step 2: Add a Shape with a Text Frame

Before formatting text, a shape must contain a TextFrame. Use shape.addTextFrame() to create one.

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().get(0);
    IAutoShape shape = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 50, 50, 500, 150
    );
    ITextFrame tf = shape.addTextFrame("Default text: will be formatted");
    prs.save("output.pptx", SaveFormat.PPTX);
}

Step 3: Access the TextFrame

shape.addTextFrame() returns the TextFrame object. You can also retrieve it later via shape.getTextFrame().

ITextFrame tf = shape.getTextFrame();          // if the frame already exists
ITextFrame tf = shape.addTextFrame("");        // creates a new frame

A TextFrame contains a list of Paragraph objects (tf.getParagraphs()). Each Paragraph contains Portion objects (paragraph.getPortions()).


Step 4: Apply Bold and Italic Formatting

Use portionFormat.setFontBold() and portionFormat.setFontItalic(). These methods accept NullableBool.TRUE, NullableBool.FALSE, or NullableBool.NOT_DEFINED (inherit from master).

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().get(0);
    IAutoShape shape = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 50, 50, 500, 150
    );
    ITextFrame tf = shape.addTextFrame("Bold and italic text");

    IPortionFormat fmt = tf.getParagraphs().get(0)
        .getPortions().get(0).getPortionFormat();
    fmt.setFontBold(NullableBool.TRUE);
    fmt.setFontItalic(NullableBool.TRUE);

    prs.save("bold-italic.pptx", SaveFormat.PPTX);
}

Step 5: Set Font Size and Color

Set portionFormat.setFontHeight() for size (in points) and use getFillFormat() for color.

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().get(0);
    IAutoShape shape = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 50, 50, 500, 150
    );
    ITextFrame tf = shape.addTextFrame("Large corporate-blue heading");

    IPortionFormat fmt = tf.getParagraphs().get(0)
        .getPortions().get(0).getPortionFormat();
    fmt.setFontHeight(32);                          // 32pt font
    fmt.setFontBold(NullableBool.TRUE);
    fmt.getFillFormat().setFillType(FillType.SOLID);
    fmt.getFillFormat().getSolidFillColor().setColor(
        Color.fromArgb(255, 0, 70, 127)
    );

    prs.save("colored-text.pptx", SaveFormat.PPTX);
}

Color.fromArgb(alpha, red, green, blue) accepts values 0-255 for each channel.


Step 6: Multiple Portions in One Paragraph

A single paragraph can contain multiple portions with different formatting. Add a new Portion to a paragraph’s getPortions() collection:

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().get(0);
    IAutoShape shape = slide.getShapes().addAutoShape(
        ShapeType.RECTANGLE, 50, 50, 600, 100
    );
    ITextFrame tf = shape.addTextFrame("");  // start with empty frame

    IParagraph paragraph = tf.getParagraphs().get(0);

    // First portion: normal text
    IPortion portion1 = paragraph.getPortions().get(0);
    portion1.setText("Normal text followed by ");
    portion1.getPortionFormat().setFontHeight(20);

    // Second portion: bold red text
    Portion portion2 = new Portion("bold red text");
    portion2.getPortionFormat().setFontHeight(20);
    portion2.getPortionFormat().setFontBold(NullableBool.TRUE);
    portion2.getPortionFormat().getFillFormat().setFillType(FillType.SOLID);
    portion2.getPortionFormat().getFillFormat().getSolidFillColor().setColor(
        Color.fromArgb(255, 200, 0, 0)
    );
    paragraph.getPortions().add(portion2);

    prs.save("mixed-format.pptx", SaveFormat.PPTX);
}

Common Issues and Fixes

Text appears black even after setting color

Ensure getFillFormat().setFillType(FillType.SOLID) is called before assigning the color. Without setting the fill type, the color change may have no effect.

NullableBool.TRUE vs true

portionFormat.setFontBold() expects NullableBool.TRUE, not Java’s true. Passing a boolean will cause a compilation error since the method expects the NullableBool enum.

Font does not appear in the saved file

The setLatinFont() method sets the Latin font family. If not set, the presentation theme font is used. Custom fonts must be embedded or available on the viewing machine.


Frequently Asked Questions

How do I change the font family?

Set portionFormat.setLatinFont():

fmt.setLatinFont(new FontData("Arial"));

FontData accepts the font family name as a string.

How do I set paragraph alignment?

Use paragraphFormat.setAlignment():

import org.aspose.slides.foss.TextAlignment;

tf.getParagraphs().get(0).getParagraphFormat()
    .setAlignment(TextAlignment.CENTER);

Supported values: LEFT, CENTER, RIGHT, JUSTIFY.

How do I set line spacing?

Use paragraphFormat.setSpaceBefore() (points before paragraph) or paragraphFormat.setSpaceAfter() (points after paragraph):

tf.getParagraphs().get(0).getParagraphFormat().setSpaceBefore(12);  // 12pt before
tf.getParagraphs().get(0).getParagraphFormat().setSpaceAfter(6);    // 6pt after

See Also