Java如何在 Word 中設定上、下標
阿新 • • 發佈:2020-09-18
上標是指比同一行中其他文字稍高的文字,而下標是指比同一行中其他文字稍低的文字。在生活中,我們常見的平方米、立方米等符號以及化學中的各種元素符號,都是利用上、下標來表示的。本文將介紹通過Free Spire.Doc for Java給Word文件中的指定字元或字串設定上標或者下標的方法。
Jar包匯入
方法一:下載Free Spire.Doc for Java包並解壓縮,然後將lib資料夾下的Spire.Doc.jar包作為依賴項匯入到Java應用程式中。
方法二:通過Maven倉庫安裝JAR包,配置pom.xml檔案的程式碼如下:
<repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>2.7.3</version> </dependency> </dependencies>
Java程式碼
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.SubSuperScript; public class SubSuperScritp { public static void main(String[] args) { //建立文件 Document doc = new Document(); Section sec = doc.addSection(); //新增段落1,設定上標 Paragraph para1 = sec.addParagraph(); para1.appendText("A"); para1.appendText("2").getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script); para1.appendText("+B"); para1.appendText("2").getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script); para1.appendText("=C"); para1.appendText("2").getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script); //新增段落2、3,設定下標 Paragraph para2 = sec.addParagraph(); para2.appendText("An = S"); para2.appendText("n").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script); para2.appendText("- S"); para2.appendText("n-1").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script); Paragraph para3 = sec.addParagraph(); para3.appendText("C"); para3.appendText("O"); para3.appendText("2").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script); //儲存文件 doc.saveToFile("SubSuperScript.docx",FileFormat.Docx_2013); doc.dispose(); } }
上下標設定效果:
以上就是Java如何在 Word 中設定上、下標的詳細內容,更多關於Java設定上、下標的資料請關注我們其它相關文章!