Monday, 6 December 2021

Add Superscript and Subscript to PowerPoint in Java

Frequently, superscript and subscript are used when making lesson plans for math and chemistry in PowerPoint. A superscript refers to text that is slightly higher than other text on the same line, while a subscript indicates text that is slightly lower than other text on the same line. This article will introduce how to add superscript and subscript to a PowerPoint slide using Java codes.

DEPENDENCY

In this tutorial, we’ll use a free third-party library called Free Spire.Presentation for Java. First of all, we need to download the product package from the link, and manually add the Spire.Presentation.jar file as a dependency in the Java program. Or we can create a Maven project and add the following code to the 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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODE

Free 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 are, the higher the superscript will appear above your text. The smaller the negative value are, the lower the subscript will appear below your text.

The following are the steps to achieve the operations:

l  Create a Presentation instance and load a sample PowerPoint document using Presentation.loadFromFile() method.

l  Get a specific slide of the file using Presentation.getSlides().get() method.

l  Add a shape to the slide using ISlide.getShapes().appendShape() method and set shape fill type and line color.

l  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.

l  Create a paragraph using ParagraphEx class, and add normal text to the paragraph using ParagraphEx.setText() method.

l  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.

l  Set text color, font and font size for the normal text and the superscript or subscript text.

l  Append the paragraph to the text frame of the shape using ITextFrameProperties.getParagraphs().append() method.

l  Save the document to another file using Presentation.saveToFile() method.

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("C:\\Users\\Test1\\Desktop\\sample.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(200, 200, 400, 150));
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("output/AddSuperscriptAndSubscript.pptx", FileFormat.PPTX_2013);
}
}


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...