How to insert formulas into PowerPoint slides through Java code
Whether you are a student, teacher, or worker, it is likely that you need to insert some formulas when creating PowerPoint documents. The inserted formula can effectively provide theoretical support for argument and calculation of the slide content. This article will show you how to programmatically insert formulas into PowerPoint slides through Java code. The following is my compilation of methods and specific steps for inserting formulas, with Java code attached for your reference.
Programming Environment
First, 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.
Method 1:
Introduced manually.
Download Free Spire. Presentation for Java locally, unzip
it, and find the Spire. Presentation.jar file in the lib folder.
Open the following interface in IDEA, and import the jar file in the local path
into the Java program:
Method 2:
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.
<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>5.1.0</version>
</dependency>
</dependencies>
Insert Formulas into PowerPoint Slides
- Initialize an instance of the Presentation class
- Using Presentation.getSlides().get(int index) method to add shapes to the slide.
- Using ISlide.getShapes().appendShape(ShapeType shapeType, Rectangle2D rectangle) method to add shapes to the slide.
- After deleting the default paragraphs in the shape through the IAutoShape.getTextFrame().getParagraphs().clear() method, add formulas to the shape through the IAutoShape.getTextFrame().getParagraphs().addParagraphFromLatexMathCode(String latexMathCode) method.
- Using Presentation.saveToFile(String file, FileFormat fileFormat) method to save the document to the specified path.
Full Code
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
public class AddFormula {
public static void main(String[] args) throws Exception{
//Define LaTeX formula code
String latexCode1 = "x^{2} + \\sqrt{x^{2}+1}=2";
String latexCode2 = "F(x) &= \\int^a_b \frac{1}{3}x^3";
String latexCode3 = "\\alpha + \\beta \\geq \\gamma";
String latexCode4 = "\\overrightarrow{abc}";
String latexCode5 =" H_x=\\frac{1}{3}\\times{ \\left[ \\begin{array}{ccc}1 & 0 & -1\\\\1 & 0 & -1\\\\1 & 0 & -1\\end{array} \\right ]}";
String latexCode6 = "\\log_a{b}";
//Initialize an instance of the Presentation class
Presentation ppt = new Presentation();
//Get the first slide
ISlide slide = ppt.getSlides().get(0);
//Add a Shape to Slide
IAutoShape shape = slide.getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(30, 100, 320, 50));
shape.getTextFrame().getParagraphs().clear();
//Using LaTeX code to add mathematical formulas to shapes
shape.getTextFrame().getParagraphs().addParagraphFromLatexMathCode(latexCode1);
//Repeat the above operation to add a shape and add a formula to the shape
shape = slide.getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(280, 80, 380, 70));
shape.getTextFrame().getParagraphs().clear();
shape.getTextFrame().getParagraphs().addParagraphFromLatexMathCode(latexCode2);
shape = slide.getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(60, 190, 240, 40));
shape.getTextFrame().getParagraphs().clear();
shape.getTextFrame().getParagraphs().addParagraphFromLatexMathCode(latexCode3);
shape = slide.getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(350, 190, 200, 40));
shape.getTextFrame().getParagraphs().clear();
shape.getTextFrame().getParagraphs().addParagraphFromLatexMathCode(latexCode4);
shape = slide.getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(10, 240, 400, 100));
shape.getTextFrame().getParagraphs().clear();
shape.getTextFrame().getParagraphs().addParagraphFromLatexMathCode(latexCode5);
shape = slide.getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(350, 280, 200, 40));
shape.getTextFrame().getParagraphs().clear();
shape.getTextFrame().getParagraphs().addParagraphFromLatexMathCode(latexCode6);
//Set shape border and fill type
for (int i = 0; i < slide.getShapes().getCount(); i++)
{
slide.getShapes().get(i).getFill().setFillType(FillFormatType.NONE);
slide.getShapes().get(i).getLine().setFillType(FillFormatType.NONE);
}
//Save the Document
ppt.saveToFile("InsertFormulas.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}
Effective Shot
Conclusion:
In this post, you have learned how to insert formulas into PowerPoint
slides in Java. Not only that, we also have other functions, such as, Java:
Copy Slides Between Two PowerPoint Documents, Java:
Add, Hide or Delete Slides in PowerPoint
and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire. Presentation for Java.
Comments
Post a Comment