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);
    }
}

输出:

国产PPT格式控件Spire.Presentation功能演示:在Java中向 PowerPoint 添加上标和下标

猜你喜欢

转载自blog.csdn.net/Augenstern__zyx/article/details/121519872