Tuesday, 03 September 2019 08:58

Set and Get Alternative Text (Alt Text) of PowerPoint Shapes in Java

Written by  support iceblue
Rate this item
(0 votes)

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);
    }
}

Set and Get Alternative Text (Alt Text) of PowerPoint Shapes in Java

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);
    }
}

Set and Get Alternative Text (Alt Text) of PowerPoint Shapes in Java

Additional Info

  • tutorial_title: