Add Superscript and Subscript to PowerPoint Using Java

<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>5.1.0</version>
</dependency>
</dependencies>
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;

import java.awt.*;

public class AddSuperscriptAndSubscript {
public static void main(String []args) throws Exception {

//Load a PowerPoint document
Presentation presentation = new Presentation();
presentation.loadFromFile("template.pptx");

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

//Add a shape to the slide
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle(150, 100, 200, 50));
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(Color.white);

//Access the text frame of the shape
ITextFrameProperties textFrame = shape.getTextFrame();
//Clear the default paragraph in the text frame
textFrame.getParagraphs().clear();

//Create a paragraph with normal text
ParagraphEx para = new ParagraphEx();
para.setText("E=mc");

//Create a portion with superscript text
PortionEx tr = new PortionEx("2");
tr.getFormat().setScriptDistance(40);

//Append the portion to the paragraph
para.getTextRanges().append(tr);

para.getTextRanges().append(new PortionEx("\n"));

//Set text color, font and font size for the normal text
tr = para.getTextRanges().get(0);
tr.getFill().setFillType(FillFormatType.SOLID);
tr.getFill().getSolidColor().setColor(new Color(128,0,128));
tr.setFontHeight(20);
tr.setLatinFont(new TextFont("Arial"));

//Set text color and font for the superscript text
tr = para.getTextRanges().get(1);
tr.getFill().setFillType(FillFormatType.SOLID);
tr.getFill().getSolidColor().setColor(Color.BLUE);
tr.setLatinFont(new TextFont("Arial"));

//Append the paragraph to the text frame of the shape
textFrame.getParagraphs().append(para);

//Create another paragraph with normal text
para = new ParagraphEx();
para.setText("X");

//Create a portion with subscript text
tr = new PortionEx("100");
tr.getFormat().setScriptDistance(-25);

//Append the portion to the paragraph
para.getTextRanges().append(tr);

//Set text color, font and font size for the normal text
tr = para.getTextRanges().get(0);
tr.getFill().setFillType(FillFormatType.SOLID);
tr.getFill().getSolidColor().setColor(new Color(128,0,128));
tr.setFontHeight(20);
tr.setLatinFont(new TextFont("Arial"));

//Set text color and font for the subscript text
tr = para.getTextRanges().get(1);
tr.getFill().setFillType(FillFormatType.SOLID);
tr.getFill().getSolidColor().setColor(Color.BLUE);
tr.setLatinFont(new TextFont("Arial"));

//Append the paragraph to the text frame of the shape
textFrame.getParagraphs().append(para);

//Save the result document
presentation.saveToFile("AddSuperscriptAndSubscript.pptx", FileFormat.PPTX_2013);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store