关于java:Java在PowerPoint中添加上标和下标

9次阅读

共计 3555 个字符,预计需要花费 9 分钟才能阅读完成。

前言

当咱们在演示文稿中增加商标、版权或其余符号时,咱们可能心愿该符号呈现在某个文本的上方或下方。在 Microsoft PowerPoint 中,咱们能够通过对符号利用上标或下标格局来实现这种成果。在这篇文章中,我将演示如何在 Java 中应用 Spire.Presentation for Java 以编程的形式实现这一工作。


程序环境

装置 Spire.Presentation for Java

首先,你须要在你的 Java 程序中增加 Spire.Presentation.jar 文件作为一个依赖项。该 JAR 文件能够从这个链接下载。如果你应用 Maven,则能够通过在 pom.xml 文件中增加以下代码轻松导入该 JAR 文件。

代码示例

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

留神:请放弃下面代码中的版本号与下载链接中的统一,以体验新性能或防止 BUG。

增加上标和下标

Spire.Presentation for Java 提供了 PortionEx.getFormat().setScriptDistance(float value) 办法来利用上标或下标格局到文本。该值能够被设置为正值或负值。正值越大,上标将在你的文本上方越高的地位呈现。负值越小,下标就会在你的文本下方越低的中央呈现。

步骤

  • 创立一个 Presentation 实例,并应用 Presentation.loadFromFile() 办法加载一个 PowerPoint 文档。
  • 应用 Presentation.getSlides().get() 办法取得想要的幻灯片。
  • 应用 ISlide.getShapes().appendShape() 办法在幻灯片上增加一个形态,并设置形态的填充类型和线条色彩。
  • 应用 IAutoShape.getTextFrame() 办法拜访形态的文本框,而后应用 ITextFrameProperties.getParagraphs().clear() 办法革除文本框中的默认段落。
  • 应用 ParagraphEx 类创立一个段落,并应用 ParagraphEx.setText() 办法向该段落增加失常文本。
  • 应用 PortionEx 类创立一个带有文本的局部,而后应用 PortionEx.getFormat().setScriptDistance(float value) 办法将上标或下标格式化到文本中。
  • 为失常文本和上标或下标文本设置文本色彩、字体和字体大小。
  • 应用 ITextFrameProperties.getParagraphs().append() 办法将段落附加到形态的文本框中。
  • 应用 Presentation.saveToFile() 办法保留后果文档。

代码示例

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;

import java.awt.*;

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

        // 加载一个 PowerPoint 文档
        Presentation presentation = new Presentation();
        presentation.loadFromFile("template.pptx");

        // 失去第一张幻灯片
        ISlide slide = presentation.getSlides().get(0);

        // 在幻灯片上增加一个形态
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle(150, 100, 200, 50));
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        // 拜访形态的文本框
        ITextFrameProperties textFrame = shape.getTextFrame();

        // 革除文本框中的默认段落
        textFrame.getParagraphs().clear();

        // 创立一个段落并增加失常文本
        ParagraphEx para = new ParagraphEx();
        para.setText("s=πr");
        
        // 创立带有上标文本的局部
        PortionEx tr = new PortionEx("2");
        tr.getFormat().setScriptDistance(40);
        
        // 增加这个局部到段落中
        para.getTextRanges().append(tr);
        
        para.getTextRanges().append(new PortionEx("\n"));

        // 为失常文本设置文本色彩,字体,字体大小
        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"));
        
        // 为上标文本设置文本色彩以及字体
        tr = para.getTextRanges().get(1);
        tr.getFill().setFillType(FillFormatType.SOLID);
        tr.getFill().getSolidColor().setColor(Color.BLUE);
        tr.setLatinFont(new TextFont("Arial"));

        // 增加段落到形态的文本框
        textFrame.getParagraphs().append(para);
        
        // 应用失常文本创立另一个段落
        para = new ParagraphEx();
        para.setText("h");
        
        // 创立带有下标文本的局部
        tr = new PortionEx("1");
        tr.getFormat().setScriptDistance(-25);
        
        // 增加这个局部到段落中
        para.getTextRanges().append(tr);

        // 为失常文本设置文本色彩,字体,字体大小
        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"));
        
        // 为下标文本设置文本色彩以及字体
        tr = para.getTextRanges().get(1);
        tr.getFill().setFillType(FillFormatType.SOLID);
        tr.getFill().getSolidColor().setColor(Color.BLUE);
        tr.setLatinFont(new TextFont("Arial"));

        // 增加这个段落到形态的文本框
        textFrame.getParagraphs().append(para);

        // 保留后果文档
        presentation.saveToFile("AddSuperscriptAndSubscript.pptx", FileFormat.PPTX_2013);
    }
}

效果图

注:该 JAR 包分为免费版和商业版,免费版没有水印或评估信息,然而有篇幅和大小限度,商业版有水印或评估信息,没有篇幅限度,想要去除这些评估信息,须要利用 license, 能够点击这里获取 30 天收费 license。

—THE END—

正文完
 0