https://kathleen1974.wordpress.com/category/itext-pdf/

In one of my project, we need to give the user a web UI (a textbox) to enter some text and allow input of superscript
and subscript tag <sup> and <sub>.
And this text entered by user will be printed on a PDF document at the backend, using iText PDF.
To print the superscript and subscript in iText PDF, we need to use the Chunk's setTextRise() method.
Below is the function I use to transform the user input string into correct iText Phrase object, which will print out
the superscript or subscript accordingly. To use these codes, copy them to your java file that performs the PDF generation, and call the function
getSubOrSupChunks(String temp, Font textFt, Font textriseFt)
passing in the input string, normal text font, and the font for superscript/subscript text, to obtain the Phrase object. private String nextSubOrSupTag(String input) {
int supIdx = StringUtils.indexOf(input, Constants.SUPERSCRIPT);
int subIdx = StringUtils.indexOf(input, Constants.SUBSCRIPT); if (subIdx == StringUtils.INDEX_NOT_FOUND && supIdx == StringUtils.INDEX_NOT_FOUND) {
return null;
} else if (subIdx == StringUtils.INDEX_NOT_FOUND) {
return Constants.SUPERSCRIPT;
} else if (supIdx == StringUtils.INDEX_NOT_FOUND) {
return Constants.SUBSCRIPT;
} else {
if (subIdx < supIdx) {
return Constants.SUBSCRIPT;
} else {
return Constants.SUPERSCRIPT;
}
}
}
private String nextEndTag(String tag) {
if (Constants.SUPERSCRIPT.equalsIgnoreCase(tag)) {
return Constants.SUPERSCRIPT_END;
} else {
return Constants.SUBSCRIPT_END;
}
} public Phrase getSubOrSupChunks(String temp, Font textFt, Font textriseFt) {
Phrase phrase = new Phrase();
String nextTag = nextSubOrSupTag(temp);
String endTag = nextEndTag(nextTag);
int tagCount = StringUtils.countMatches(temp, Constants.SUBSCRIPT) + StringUtils.countMatches(temp, Constants.SUPERSCRIPT);
for (int i=0;i<tagCount;i++) {
logger.debug("i i);
if (i == 0) {
phrase.add(new Chunk(StringUtils.substringBefore(temp, nextTag), textFt));
} else {
temp = StringUtils.substringAfter(temp, nextTag);
nextTag = nextSubOrSupTag(temp);
phrase.add(new Chunk(StringUtils.substringBetween(temp, endTag, nextTag), textFt));
endTag = nextEndTag(nextTag);
}
if (Constants.SUBSCRIPT.equalsIgnoreCase(nextTag)) {
phrase.add(new Chunk(StringUtils.substringBetween(temp, nextTag, endTag), textriseFt).setTextRise(-3f));
} else {
phrase.add(new Chunk(StringUtils.substringBetween(temp, nextTag, endTag), textriseFt).setTextRise(5f));
}
if (i == tagCount -1) {
temp = StringUtils.substringAfter(temp, nextTag);
phrase.add(new Chunk(StringUtils.substringAfter(temp, endTag), textFt));
}
}
return phrase;
} The content of Constants.java are: public static final String SUBSCRIPT = "<sub>";
public static final String SUBSCRIPT_END = "</sub>";
public static final String SUPERSCRIPT = "<sup>";
public static final String SUPERSCRIPT_END = "</sup>";

自己来拿改改如下:

public class PdfUtil {
private static Logger logger = LoggerFactory.getLogger(PdfUtil.class); /** 处理 数字的上标 和 小标 */
public static final String SUBSCRIPT = "<sub>";
public static final String SUBSCRIPT_END = "</sub>";
public static final String SUPERSCRIPT = "<sup>";
public static final String SUPERSCRIPT_END = "</sup>";
public static Font fontGeneral; // 一般内容 private static String nextSubOrSupTag(String input) {
int supIdx = StringUtils.indexOf(input, SUPERSCRIPT);
int subIdx = StringUtils.indexOf(input, SUBSCRIPT); if (subIdx == StringUtils.INDEX_NOT_FOUND && supIdx == StringUtils.INDEX_NOT_FOUND) {
return null;
} else if (subIdx == StringUtils.INDEX_NOT_FOUND) {
return SUPERSCRIPT;
} else if (supIdx == StringUtils.INDEX_NOT_FOUND) {
return SUBSCRIPT;
} else {
if (subIdx < supIdx) {
return SUBSCRIPT;
} else {
return SUPERSCRIPT;
}
}
} private static String nextEndTag(String tag) {
if (SUPERSCRIPT.equalsIgnoreCase(tag)) {
return SUPERSCRIPT_END;
} else {
return SUBSCRIPT_END;
}
} public static Phrase getSubOrSupChunks(String temp, Font textFt, Font textriseFt) {
Phrase phrase = new Phrase();
String nextTag = nextSubOrSupTag(temp);
String endTag = nextEndTag(nextTag);
int tagCount = StringUtils.countMatches(temp, SUBSCRIPT) + StringUtils.countMatches(temp, SUPERSCRIPT);
for (int i=0;i<tagCount;i++) {
logger.debug("i:" + i);
if (i == 0) {
phrase.add(new Chunk(StringUtils.substringBefore(temp, nextTag), textFt));
} else {
temp = StringUtils.substringAfter(temp, nextTag);
nextTag = nextSubOrSupTag(temp);
phrase.add(new Chunk(StringUtils.substringBetween(temp, endTag, nextTag), textFt));
endTag = nextEndTag(nextTag);
}
if (SUBSCRIPT.equalsIgnoreCase(nextTag)) {
phrase.add(new Chunk(StringUtils.substringBetween(temp, nextTag, endTag), textriseFt).setTextRise(-3f));
} else {
phrase.add(new Chunk(StringUtils.substringBetween(temp, nextTag, endTag), textriseFt).setTextRise(5f));
}
if (i == tagCount -1) {
temp = StringUtils.substringAfter(temp, nextTag);
phrase.add(new Chunk(StringUtils.substringAfter(temp, endTag), textFt));
}
}
return phrase;
}
}

使用方法:

		Phrase phrase = PdfUtil.getSubOrSupChunks(j62.getString("j6202"),fontGeneral,fontGeneral);
p = new Paragraph(phrase);
p.setAlignment(Element.ALIGN_LEFT);
cellC.addElement(p);
cellC.setColspan(3);
tableContent.addCell(cellC);

将含有 数字科学计数法 有上标 或者小标的 字符串经过 PdfUtil.getSubOrSupChunks() 处理之后,得到一个 Phrase,然后使用它初始化得到一个 Paragraph对象,就可以加入到 表格中了。

原理是调用:

new Chunk(xxx, textriseFt).setTextRise(-3f); //小标
new Chunk(xxx), textriseFt).setTextRise(5f); //上标

itext 实现pdf打印数字上标和下标的更多相关文章

  1. 开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍之八)

    在这个.NET组件的介绍系列中,受到了很多园友的支持,一些园友(如:数据之巅. [秦时明月]等等这些大神 )也给我提出了对应的建议,我正在努力去改正,有不足之处还望大家多多包涵.在传播一些简单的知识的 ...

  2. 如何在程序中给word文档加上标和下标

    如何在程序中给word文档加上标和下标 上标或下标是一个小于普通行格式的数字,图形,标志或者指示通常它的设置与行相比偏上或偏下.下标通常显示于或者低于基准线,而上标则高于.上标和下标通常被用于表达公式 ...

  3. 打出10的n次方,上标,下标等处理方法(mac)

    我使用mac系统遇到的需求,需要在项目中显示10的6次方 用来做单位,找了很多方案,word等文本编辑工具很好实现(word是使用ctrl + shift + =)(mac  版的word是 Comm ...

  4. HTML5基础知识(1)--上标和下标文本

    1.上标文本标签:<sup>/<sup> 2.下标文本标签:<sub></sub> 3.案例代码 <!doctype html> <h ...

  5. C#:IText构造PDF文件

    IText构造PDF文件 1.1 生成Document Document是我们要生成的PDF文件所有元素的容器,因此要生成一个PDF文档,必须首先定义一个Document对象. Document有三种 ...

  6. iText导出pdf、word、图片

    一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超级连接显示或 ...

  7. Itext导出PDF,word,图片案例

    iText导出pdf.word.图片 一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生 ...

  8. 面试 15:顺时针从外往里打印数字(剑指 Offer 第 20 题)

    面试 15:顺时针从外往里打印数字 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印每一个数字.例如输入: {{1,2,3}, {4,5,6}, {7,8,9}} 则依次打印数字为 1.2.3. ...

  9. SQL Server如何存储特殊字符、上标、下标

    测试验证特殊符号能否存入数据库中: 其中,像一些普通单位符号比如“ ° ′"﹩ $ ﹠ & £¥ ‰ % ℃ ¤ ¢℉”可以正常录入没有问题,但是万分号“‱”之上就不可以了,录入后显 ...

随机推荐

  1. Appcelerator Titanium Studio: JNI_CreateJavaVM missing error

    Mac升级到Yosemite后,Titanium Studio启动不了,报Appcelerator Studio: JNI_CreateJavaVM missing error 之类的错误,重装了Or ...

  2. Map遍历两种方式

    Java代码 Map<String,String> map=new HashMap<String,String>(); map.put("username" ...

  3. java 内部类 *** 最爱那水货

    注: 转载于http://blog.csdn.net/jiangxinyu/article/details/8177326 Java语言允许在类中再定义类,这种在其它类内部定义的类就叫内部类.内部类又 ...

  4. C语言Scanf函数

    C语言的scanf函数 一.变量的内存分析 (一)字节与地址 ①. 内存以字节为单位 每个字节都有自己的内存地址,根据地址就可以找到该字节.整个内存相当于一整个酒店,而酒店以房间为单位,在这里每个房间 ...

  5. Code First :使用Entity. Framework编程(4) ----转发 收藏

    第4章 对关系使用默认规则与配置 在第3章,你已经掌握了默认规则与配置对属性以及其在数据库映射的字段的影响.在本章,我们把焦点放在类之间的关系上面.这包括类在内存如何关联,还有数据库中的外键维持等.你 ...

  6. Rainyday.js – 使用 JavaScript 实现雨滴效果

    Rainyday.js 背后的想法是创建一个 JavaScript 库,利用 HTML5 Canvas 渲染一个雨滴落在玻璃表面的动画.Rainyday.js 有功能可扩展的 API,例如碰撞检测和易 ...

  7. CSS实现DIV水平 垂直居中-1

    水平大家都知道,一般固定宽度给个margin:0 auto:就可以了.下面实现水平垂直都居中 HTML <div class="parent"> </div> ...

  8. sizzle源码分析 (3)sizzle 不能快速匹配时 选择器流程

    如果快速匹配不成功,则会进入sizzle自己的解析顺序,主要流程如下: 总结流程如下: (1)函数sizzle是sizzle的入口,如果能querySelectAll快速匹配,则返回结果 (2)函数S ...

  9. Weinre调试移动端页面

    Weinre是什么 如果我们做的是Cordova(phonegap)或其他hybird应用,当使用到原生功能时候(类似原生请求数据或页面切换时),没办法在PC chrome浏览器调试页面,一旦页面在手 ...

  10. Web服务器具体开发流程

    下面是我个人对Web服务器开发流程的一点理解,下面做出了大概的模型,实现了基本的功能,下面也有所有的代码可以提供参考: 一开始学的时候不要把网站想的太复杂了,把网站的流程和大概的原理框架搞清楚,在通过 ...