當我們在演示文稿中添加商標、版權或其他符號時,我們可能希望該符號出現在某個文本的上方或下方。在Microsoft PowerPoint中,我們可以通過對符號應用上標或下標格式來實現這種效果。 ...
前言
當我們在演示文稿中添加商標、版權或其他符號時,我們可能希望該符號出現在某個文本的上方或下方。在Microsoft PowerPoint中,我們可以通過對符號應用上標或下標格式來實現這種效果。在這篇文章中,我們將演示如何在Java中使用Spire.Presentation for Java以編程的方式實現這一任務。
程式環境配置
安裝Spire.Presentation for Java
首先,你需要在你的Java程式中添加Spire.Presentation.jar文件作為一個依賴項。該JAR文件可以從這個鏈接下載。如果你使用Maven,則可以通過在pom.xml文件中添加以下代碼輕鬆導入該JAR文件。
1 <repositories> 2 <repository> 3 <id>com.e-iceblue</id> 4 <name>e-iceblue</name> 5 <url> https://repo.e-iceblue.cn/repository/maven-public /</url> 6 </repository> 7 </repositories> 8 <dependencies> 9 <dependency> 10 <groupId>e-iceblue</groupId> 11 <artifactId>spire.presentation</artifactId> 12 <version>7.9.1</version> 13 </dependency> 14 </dependencies>
註意:請保持上面代碼中的版本號與下載鏈接中的一致,以體驗新功能或避免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 import com.spire.presentation.*; 2 import com.spire.presentation.drawing.*; 3 4 import java.awt.*; 5 6 public class AddSuperscriptAndSubscript { 7 public static void main(String []args) throws Exception { 8 9 //載入一個PowerPoint文檔 10 Presentation presentation = new Presentation(); 11 presentation.loadFromFile("template.pptx"); 12 13 //得到第一張幻燈片 14 ISlide slide = presentation.getSlides().get(0); 15 16 //在幻燈片上添加一個形狀 17 IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle(150, 100, 200, 50)); 18 shape.getFill().setFillType(FillFormatType.NONE); 19 shape.getShapeStyle().getLineColor().setColor(Color.white); 20 21 //訪問形狀的文本框 22 ITextFrameProperties textFrame = shape.getTextFrame(); 23 24 //清除文本框中的預設段落 25 textFrame.getParagraphs().clear(); 26 27 //創建一個段落並添加正常文本 28 ParagraphEx para = new ParagraphEx(); 29 para.setText("s=πr"); 30 31 //創建帶有上標文本的部分 32 PortionEx tr = new PortionEx("2"); 33 tr.getFormat().setScriptDistance(40); 34 35 //添加這個部分到段落中 36 para.getTextRanges().append(tr); 37 38 para.getTextRanges().append(new PortionEx("\n")); 39 40 //為正常文本設置文本顏色,字體,字體大小 41 tr = para.getTextRanges().get(0); 42 tr.getFill().setFillType(FillFormatType.SOLID); 43 tr.getFill().getSolidColor().setColor(new Color(128,0,128)); 44 tr.setFontHeight(20); 45 tr.setLatinFont(new TextFont("Arial")); 46 47 //為上標文本設置文本顏色以及字體 48 tr = para.getTextRanges().get(1); 49 tr.getFill().setFillType(FillFormatType.SOLID); 50 tr.getFill().getSolidColor().setColor(Color.BLUE); 51 tr.setLatinFont(new TextFont("Arial")); 52 53 //添加段落到形狀的文本框 54 textFrame.getParagraphs().append(para); 55 56 //使用正常文本創建另一個段落 57 para = new ParagraphEx(); 58 para.setText("h"); 59 60 //創建帶有下標文本的部分 61 tr = new PortionEx("1"); 62 tr.getFormat().setScriptDistance(-25); 63 64 //添加這個部分到段落中 65 para.getTextRanges().append(tr); 66 67 //為正常文本設置文本顏色,字體,字體大小 68 tr = para.getTextRanges().get(0); 69 tr.getFill().setFillType(FillFormatType.SOLID); 70 tr.getFill().getSolidColor().setColor(new Color(128,0,128)); 71 tr.setFontHeight(20); 72 tr.setLatinFont(new TextFont("Arial")); 73 74 //為下標文本設置文本顏色以及字體 75 tr = para.getTextRanges().get(1); 76 tr.getFill().setFillType(FillFormatType.SOLID); 77 tr.getFill().getSolidColor().setColor(Color.BLUE); 78 tr.setLatinFont(new TextFont("Arial")); 79 80 //添加這個段落到形狀的文本框 81 textFrame.getParagraphs().append(para); 82 83 //保存結果文檔 84 presentation.saveToFile("AddSuperscriptAndSubscript.pptx", FileFormat.PPTX_2013); 85 } 86 }
效果圖
---THE END---