栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spire.Presentation功能演示:在Java中向 PowerPoint 添加上标和下标

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spire.Presentation功能演示:在Java中向 PowerPoint 添加上标和下标

Spire.Presentation for Java 专业的 PowerPoint API,它允许开发人员在 Java 应用程序中创建、读取、写入、转换和保存 PowerPoint 文档,而无需安装 Microsoft PowerPoint。

可点击此处下载最新版测试。

当我们在演示文稿中添加商标、版权或其他符号时,我们可能希望该符号稍微高于或低于某个文本。在 Microsoft PowerPoint 中,我们可以简单地通过对符号应用上标或下标格式来实现这种效果。在本文中,我们将演示如何使用Spire.Presentation for Java以编程方式在 Java 中实现此任务。

添加上标和下标

Spire.Presentation for Java 提供了PortionEx.getFormat().setscriptDistance(float value)方法,用于将上标或下标格式应用于文本。该值可以设置为正值或负值。正值越大,上标出现在文本上方的位置就越高。负值越小,文本下方出现的下标就越低。
以下是为 PowerPoint 文档添加上标或下标的步骤:

  • 创建一个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 {

        //Load a PowerPoint document
        Presentation presentation = new Presentation();
        presentation.loadFromFile("template.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(150, 100, 200, 50));
        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("AddSuperscriptAndSubscript.pptx", FileFormat.PPTX_2013);
    }
}

输出:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/592392.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号