Java 插入公式到PPT幻燈片

2022-07-12 15:00:39

PowerPoint幻燈片中可插入公式,用於在幻燈片放映時演示相關內容的論證、推算的依據,能有效地為演講者提供論述的資料支撐。通過後端程式程式碼,我們可藉助特定的工具來實現在幻燈片中的插入公式,本文,將對此作詳細介紹。

Jar包引入

通過 Maven倉庫 下載匯入,如下設定pom.xml:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation.jar </artifactId>
        <version>7.6.3</version>
    </dependency>
</dependencies>

如需手動匯入,需要下載jar包到本地,然後解壓,找到lib資料夾下的Spire.Presentation.jar檔案。在IDEA中開啟「Project Structure」介面,執行如圖步驟將本地路徑下的jar檔案手動引入Java程式:

 

插入公式

下面是在PPT幻燈片中插入公式的主要方法和步驟:

  1. 建立Presentation類的物件,並通過Presentation.getSlides().get(int index)方法獲取第一張幻燈片。
  2. 通過ISlide.getShapes().appendShape(ShapeType shapeType, Rectangle2D rectangle)方法新增形狀到幻燈片。
  3. 通過IAutoShape.getTextFrame().getParagraphs().clear()方法刪除形狀中預設的段落後,通過IAutoShape.getTextFrame().getParagraphs().addParagraphFromLatexMathCode(String latexMathCode)方法新增公式到形狀。LaTeX公式字串可先行定義好,如本次程式碼範例。
  4. 最後,呼叫Presentation.saveToFile(String file, FileFormat fileFormat)方法儲存檔案到指定路徑。

Java

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{
        //定義LaTeX公式程式碼
        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}";

        //建立Presentation類的範例
        Presentation ppt = new Presentation();

        //獲取第一張幻燈片
        ISlide slide = ppt.getSlides().get(0);

        //新增形狀到幻燈片
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(30, 100, 320, 50));
        shape.getTextFrame().getParagraphs().clear();
        //使用LaTeX程式碼新增數學公式到形狀
        shape.getTextFrame().getParagraphs().addParagraphFromLatexMathCode(latexCode1);

        //重複以上操作,新增形狀,並新增公式到形狀
        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);

        //設定形狀邊框和填充型別
        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);
        }

        //儲存檔案
        ppt.saveToFile("MathEquations.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

公式插入效果:

 

—END—