Java如何在iText中创建上标/下标?
下面的示例将介绍如何使用iText在pdf文档中创建上标和下标文本。我们可以使用名为setTextRise()的Chunk类方法。正值将创建上标文本,负值将创建下标文本。
package org.nhooo.example.itextpdf; import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class SuperSubscriptDemo { public static void main(String[] args) { Document doc = new Document(); try { PdfWriter.getInstance(doc, new FileOutputStream("SuperSubscript.pdf")); doc.open(); Font small = FontFactory.getFont(FontFactory.HELVETICA, 5, Font.ITALIC); // 将一些块添加到doc对象中。 doc.add(new Chunk("The quick brown ")); Chunk superscript = new Chunk("fox "); superscript.setTextRise(5f); superscript.setFont(small); doc.add(superscript); doc.add(new Chunk("jumps over the lazy ")); Chunk subscript = new Chunk("dog"); subscript.setTextRise(-5f); subscript.setFont(small); doc.add(subscript); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } finally { doc.close(); } } }
Maven依赖
<!-- http://repo1.maven.org/maven2/com/itextpdf/itextpdf/5.5.10/itextpdf-5.5.10.jar --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency>