前言
当我们在演示文稿中添加商标、版权或其他符号时,我们可能希望该符号出现在某个文本的上方或下方。在Microsoft PowerPoint中,我们可以通过对符号应用上标或下标格式来实现这种效果。在这篇文章中,我们将演示如何在Java中使用Spire.Presentation for Java以编程的方式实现这一任务。
程序环境配置
安装Spire.Presentation for Java
首先,你需要在你的Java程序中添加Spire.Presentation.jar文件作为一个依赖项。该JAR文件可以从这个链接下载。如果你使用Maven,则可以通过在pom.xml文件中添加以下代码轻松导入该JAR文件。
1 | com.e-icebluee-iceblue https://repo.e-iceblue.cn/repository/maven-public /e-icebluespire.presentation7.9.1 |
注意:请保持上面代码中的版本号与下载链接中的一致,以体验新功能或避免BUG。
添加上标和下标
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()方法保存结果文档。
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 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); } } |
到此这篇关于Java在PowerPoint中添加上标和下标的文章就介绍到这了,更多相关Java添加上标和下标内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!