Java如何在 Word 中設(shè)置上、下標(biāo)
上標(biāo)是指比同一行中其他文字稍高的文字,而下標(biāo)是指比同一行中其他文字稍低的文字。在生活中,我們常見的平方米、立方米等符號以及化學(xué)中的各種元素符號,都是利用上、下標(biāo)來表示的。本文將介紹通過Free Spire.Doc for Java給Word文檔中的指定字符或字符串設(shè)置上標(biāo)或者下標(biāo)的方法。
Jar包導(dǎo)入
方法一:下載Free Spire.Doc for Java包并解壓縮,然后將lib文件夾下的Spire.Doc.jar包作為依賴項導(dǎo)入到Java應(yīng)用程序中。
方法二:通過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) { //創(chuàng)建文檔 Document doc = new Document(); Section sec = doc.addSection(); //添加段落1,設(shè)置上標(biāo) 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,設(shè)置下標(biāo) 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(); }}
上下標(biāo)設(shè)置效果:
以上就是Java如何在 Word 中設(shè)置上、下標(biāo)的詳細(xì)內(nèi)容,更多關(guān)于Java設(shè)置上、下標(biāo)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章: