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

前言

当咱们在演示文稿中增加商标、版权或其余符号时,咱们可能心愿该符号呈现在某个文本的上方或下方。在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—

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理