Java: Add Superscript and Subscript to PowerPoint
When we add a trademark, copyright or other symbol to our presentation, we might want the symbol to appear slightly above or below a certain text. In Microsoft PowerPoint, we can implement this effect simply by applying superscript or subscript formatting to the symbol. In this article, we will demonstrate how to achieve this task programmatically in Java using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>10.11.4</version>
</dependency>
</dependencies>
Add Superscript and Subscript
Spire.Presentation for Java provides the PortionEx.getFormat().setScriptDistance(float value) method for applying superscript or subscript formatting to text. The value can be set as positive or negative. The bigger the positive value, the higher the superscript will appear above your text. The smaller the negative value, the lower the subscript will appear below your text.
The following are the steps to add superscript or subscript to a PowerPoint document:
- Create a Presentation instance and load a PowerPoint document using Presentation.loadFromFile() method.
- Get the desired slide using Presentation.getSlides().get() method.
- Add a shape to the slide using ISlide.getShapes().appendShape() method and set shape fill type and line color.
- Access the text frame of the shape using IAutoShape.getTextFrame() method, then clear the default paragraph in the text frame using ITextFrameProperties.getParagraphs().clear() method.
- Create a paragraph using ParagraphEx class, and add normal text to the paragraph using ParagraphEx.setText() method.
- Create a portion with text using PortionEx class, and then apply superscript or subscript formatting to the text using PortionEx.getFormat().setScriptDistance(float value) method.
- Set text color, font and font size for the normal text and the superscript or subscript text.
- Append the paragraph to the text frame of the shape using ITextFrameProperties.getParagraphs().append() method.
- Save the result document using Presentation.saveToFile() method.
- Java
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);
}
}
Output:

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Change Font Styles in PowerPoint in Java
This article demonstrates how to change font styles (font name, font size, font color, bold, italic and underlined) of an existing PowerPoint document by using Spire.Presentation for Java.
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
public class ChangeFontStyles {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
//Load the sample PowerPoint file
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");
//Get the text shape
IAutoShape shape = (IAutoShape) presentation.getSlides().get(0).getShapes().get(0);
//Get the first paragraph and change the font color of it
ParagraphEx paragraph = shape.getTextFrame().getParagraphs().get(0);
PortionEx textRange = paragraph.getFirstTextRange();
textRange.getFormat().getFill().setFillType(FillFormatType.SOLID);
textRange.getFormat().getFill().getSolidColor().setColor(Color.blue);
//Get the second paragraph and make the text bold, italic and unlined
paragraph = shape.getTextFrame().getParagraphs().get(1);
textRange = paragraph.getFirstTextRange();
textRange.getFormat().isBold(TriState.TRUE);
textRange.getFormat().isItalic(TriState.TRUE);
textRange.getFormat().setTextUnderlineType(TextUnderlineType.DASHED);
//Get the third paragraph and change the font name and size
paragraph = shape.getTextFrame().getParagraphs().get(2);
textRange = paragraph.getFirstTextRange();
textRange.getFormat().setLatinFont(new TextFont("Segoe Print"));
textRange.getFormat().setFontHeight(22f);
//Save the document
presentation.saveToFile("output/ChangeFontStyles.pptx", FileFormat.PPTX_2013);
}
}

Set Text Alignment within a Shape in Java
This article demonstrates how to set text horizontal and vertical alignment within a shape using Spire.Presentation for Java.
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class SetTextAlignment {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//Add a shape
IAutoShape textShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50, 50, 400, 200));
textShape.getShapeStyle().getLineColor().setColor(Color.DARK_GRAY);
textShape.getFill().setFillType(FillFormatType.NONE);
//Remove the default paragraphs
textShape.getTextFrame().getParagraphs().clear();
//Add a paragraph and append some text to it
textShape.getTextFrame().getParagraphs().append(new ParagraphEx());
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().append(new PortionEx("Text Alignment"));
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setFontHeight(20f);
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Arial"));
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().getSolidColor().setColor(Color.BLACK);
//Set text horizontal alignment to right
textShape.getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
//Set text vertical alignment to bottom
textShape.getTextFrame().setAnchoringType(TextAnchorType.BOTTOM);
//Save to file
presentation.saveToFile("output/TextAlignment.pptx",FileFormat.PPTX_2013);
}
}

Get Position of Text in PowerPoint in Java
This article demonstrates how to get the position of text in PowerPoint in Java using Spire.Presentation for Java.
import com.spire.presentation.IAutoShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import java.awt.geom.Point2D;
public class GetPositionOfText {
public static void main(String []args) throws Exception {
//Create a Presentation instance
Presentation ppt = new Presentation();
//Load a PowerPoint document
ppt.loadFromFile("Sample.pptx");
//Get the first slide
ISlide slide = ppt.getSlides().get(0);
//Get the first shape
IAutoShape shape = (IAutoShape)slide.getShapes().get(0);
//Get location of text in the shape
Point2D location =shape.getTextFrame().getTextLocation();
//Print out the x and y coordinates of the location relative to slide
String point1="Text's position relative to Slide: x= "+location.getX()+" y = "+location.getY();
System.out.println(point1);
//Print out the x and y coordinates of the location relative to shape
String point2 = "Text's position relative to shape: x= " + (location.getX() - shape.getLeft()) + " y = " + (location.getY() - shape.getTop());
System.out.println(point2);
}
}
Output:

Apply Transparency to Text in PowerPoint in Java
This article demonstrates how to apply transparency to text in PowerPoint using Spire.Presentation for Java.
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class ApplyTransparency {
public static void main(String[] args) throws Exception {
//Create a PowerPoint document
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//Add a shape
IAutoShape textbox = presentation .getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(50, 70, 300, 120));
textbox.getShapeStyle().getLineColor().setColor(new Color(1,1,1,0));
textbox.getFill().setFillType(FillFormatType.NONE);
//Remove default paragraphs
textbox.getTextFrame().getParagraphs().clear();
//Add three paragraphs and apply colors with different alpha values to the text
int alpha = 55;
for (int i = 0; i < 3; i++)
{
textbox.getTextFrame().getParagraphs().append(new ParagraphEx());
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().append(new PortionEx("Text Transparency"));
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().setFillType(FillFormatType.NONE);
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().getSolidColor().setColor(new Color(176, 48, 96, alpha));
alpha += 100;
}
//Save to file
presentation.saveToFile("TextTransparency.pptx", FileFormat.PPTX_2013);
}
}

Apply a Shadow Effect to Text in PowerPoint in Java
This article demonstrates how to apply a shadow effect to the text in a PowerPoint slide using Spire.Presentation for Java.
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.OuterShadowEffect;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class SetShadowEffect {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//Get the first slide
ISlide slide = presentation.getSlides().get(0);
//Add a rectangle to slide
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(50,80,500,100));
shape.getFill().setFillType(FillFormatType.NONE);
shape.getLine().setFillType(FillFormatType.NONE);
//Set text of the shape
shape.appendTextFrame("Text shading on slide");
//Set font style
shape.getTextFrame().getTextRange().setFontHeight(38f);
shape.getTextFrame().getTextRange().setLatinFont(new TextFont("Arial Black"));
shape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
shape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);
//Create a OuterShadowEffect object
OuterShadowEffect outerShadow= new OuterShadowEffect();
//Set the shadow effect
outerShadow.setBlurRadius(0);
outerShadow.setDirection(50);
outerShadow.setDistance(10);
outerShadow.getColorFormat().setColor(Color.orange);
//Apply shadow effect to text
shape.getTextFrame().getTextRange().getEffectDag().setOuterShadowEffect(outerShadow);
//Save to file
presentation.saveToFile("output/AddShadow.pptx", FileFormat.PPTX_2013);
}
}

Add Animation Effect to Paragraph in PowerPoint in Java
We have already introduced how to add animation effect to shape in PowerPoint, in this article, we will introduce how to add animation effect to paragraph in PowerPoint using Spire.Presentation for Java.
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.animation.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class AddAnimationOnParagraph {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation ppt = new Presentation();
//Get the first slide
ISlide slide = ppt.getSlides().get(0);
//Add a shape to the slide
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(150, 150, 450, 100));
shape.getFill().setFillType(FillFormatType.SOLID);
shape.getFill().getSolidColor().setColor(Color.gray);
shape.getShapeStyle().getLineColor().setColor(Color.white);
shape.appendTextFrame("This demo shows how to apply animation on paragraph in PPT document.");
//Add animation effect to the first paragraph in the shape
AnimationEffect animation = shape.getSlide().getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.FLOAT);
animation.setStartEndParagraphs(0, 0);
//Save the result document
ppt.saveToFile("AddAnimationOnPara.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}
Output:

Insert HTML with images into PowerPoint in Java
We have already demonstrated how to insert HTML formatted text to a Presentation slide by using Spire.Presentation for Java. This article will introduce the way to insert HTML with images to PowerPoint and each html tag will be added to the slide as a separate shape.
import com.spire.presentation.*;
import com.spire.presentation.collections.*;
public class AddHTMLWithImage {
public static void main(String[] args) throws Exception {
//Create an instance of presentation document
Presentation ppt = new Presentation();
//Get the shapes on the first slide.
ShapeList shapes = ppt.getSlides().get(0).getShapes();
//Add contents to shapes from HTML codes, which includes text and image.
shapes.addFromHtml("<html><div><p>E-iceblue</p>" +
"<p><img src='https://cdn.e-iceblue.com/C:\\Users\\Test1\\Desktop\\logo.png'/></p>" +
"<p>Spire.Presentation for Java</p></html>");
//Save the document
String result = "output/insertHtmlWithImage.pptx";
ppt.saveToFile(result, FileFormat.PPTX_2013);
}
}

Java: Highlight Text in PowerPoint
When you want to emphasize a particular point in a PowerPoint presentation, you can highlight it with a bright color to help the audience catch it at first glance. In this article, we will explain how to highlight text in a PowerPoint presentation in Java using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>10.11.4</version>
</dependency>
</dependencies>
Highlight Text in PowerPoint in Java
The following are the steps to highlight specific text in a PowerPoint document:
- Initialize an instance of Presentation class.
- Load a PowerPoint presentation using Presentation.loadFromFile() method.
- Loop through the slides in the presentation and the shapes on each slide.
- Check if the current shape is of IAutoShape type.
- If the result is true, typecast it to IAutoShape.
- Initialize an instance of TextHighLightingOptions class, and set the text highlighting options such as whole words only and case sensitive using TextHighLightingOptions.setWholeWordsOnly() and TextHighLightingOptions.setCaseSensitive() methods.
- Highlight a specific text in the shape using IAutoShape.getTextFrame().highLightText() method.
- Save the result file using Presentation.saveToFile() method.
- Java
import com.spire.presentation.*;
import java.awt.*;
public class HighlightTextInPPT {
public static void main(String []args) throws Exception {
//Create an instance of Presentation class
Presentation presentation = new Presentation();
//Load a PowerPoint file
presentation.loadFromFile("Input.pptx");
//Loop through all slides
for (int i = 0; i < presentation.getSlides().getCount(); i++)
{
//Get the current slide
ISlide slide = presentation.getSlides().get(i);
//Loop through the shapes on the slide
for (int j = 0; j < slide.getShapes().getCount(); j++)
{
//Check if the current shape is of IAutoShape type
if (slide.getShapes().get(j) instanceof IAutoShape)
{
//Typecast the shape to IAutoShape
IAutoShape shape = (IAutoShape)slide.getShapes().get(j);
//Create an instance of TextHighLightingOptions class
TextHighLightingOptions options = new TextHighLightingOptions();
//Set text highlighting options
options.setCaseSensitive(true);
options.setWholeWordsOnly(true);
//Highlight specific text within the shape with color
shape.getTextFrame().highLightText("Spire", Color.YELLOW, options);
}
}
}
//Save the result file
presentation.saveToFile("HighlightText.pptx", FileFormat.PPTX_2013);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Java set the intents and spacing for paragraph on the presentation slide
There are two kinds of special indents styles for the paragraph on the presentation slides, first line and hanging. This article will demonstrate how to set the indents and spacing for the paragraph on presentation slide in Java applications.
import com.spire.presentation.*;
public class IndentStyle {
public static void main(String[] args) throws Exception{
//Load the sample document
Presentation presentation = new Presentation();
presentation.loadFromFile("Indent.pptx");
//get the shapes
IAutoShape shape = (IAutoShape) presentation.getSlides().get(0).getShapes().get(0);
//set the indent, margin and space for the first paragraph
shape.getTextFrame().getParagraphs().get(0).setIndent(20);
shape.getTextFrame().getParagraphs().get(0).setLeftMargin(10);
shape.getTextFrame().getParagraphs().get(0).setSpaceAfter(10);
//set the indent, margin and space for the third paragraph
shape.getTextFrame().getParagraphs().get(2).setIndent(-100);
shape.getTextFrame().getParagraphs().get(2).setLeftMargin(40);
shape.getTextFrame().getParagraphs().get(2).setSpaceBefore(0);
shape.getTextFrame().getParagraphs().get(2).setSpaceAfter(0);
//save the document to file
String output = "output/result.pptx";
presentation.saveToFile(output, FileFormat.PPTX_2010);
}
}
Effective screenshot after setting the indent style of paragraphs on presentation slide:
