itext 实现pdf打印数字上标和下标
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打印数字上标和下标的更多相关文章
- 开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍之八)
在这个.NET组件的介绍系列中,受到了很多园友的支持,一些园友(如:数据之巅. [秦时明月]等等这些大神 )也给我提出了对应的建议,我正在努力去改正,有不足之处还望大家多多包涵.在传播一些简单的知识的 ...
- 如何在程序中给word文档加上标和下标
如何在程序中给word文档加上标和下标 上标或下标是一个小于普通行格式的数字,图形,标志或者指示通常它的设置与行相比偏上或偏下.下标通常显示于或者低于基准线,而上标则高于.上标和下标通常被用于表达公式 ...
- 打出10的n次方,上标,下标等处理方法(mac)
我使用mac系统遇到的需求,需要在项目中显示10的6次方 用来做单位,找了很多方案,word等文本编辑工具很好实现(word是使用ctrl + shift + =)(mac 版的word是 Comm ...
- HTML5基础知识(1)--上标和下标文本
1.上标文本标签:<sup>/<sup> 2.下标文本标签:<sub></sub> 3.案例代码 <!doctype html> <h ...
- C#:IText构造PDF文件
IText构造PDF文件 1.1 生成Document Document是我们要生成的PDF文件所有元素的容器,因此要生成一个PDF文档,必须首先定义一个Document对象. Document有三种 ...
- iText导出pdf、word、图片
一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超级连接显示或 ...
- Itext导出PDF,word,图片案例
iText导出pdf.word.图片 一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生 ...
- 面试 15:顺时针从外往里打印数字(剑指 Offer 第 20 题)
面试 15:顺时针从外往里打印数字 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印每一个数字.例如输入: {{1,2,3}, {4,5,6}, {7,8,9}} 则依次打印数字为 1.2.3. ...
- SQL Server如何存储特殊字符、上标、下标
测试验证特殊符号能否存入数据库中: 其中,像一些普通单位符号比如“ ° ′"﹩ $ ﹠ & £¥ ‰ % ℃ ¤ ¢℉”可以正常录入没有问题,但是万分号“‱”之上就不可以了,录入后显 ...
随机推荐
- ubuntu下golang环境配置
安装go 可以到Golang中国下载go的安装包 解压安装包tar -C /usr/local -xzf <安装包> 添加环境变量`export PATH=$PATH:/usr/local ...
- 使用MD5WithRSA来签名和验签(.NET)
/// <summary> /// 数字签名并返回BASE64编码的字符串 /// </summary> /// <param name="contentFor ...
- 学习HTML5之表单
HTML5 的标准已经定了,应该火了,或者已经火了.那么是不是可以学习一下呢? 目前h5的主场还是在手机端,pc还是受困于浏览器的兼容,主要是IE在拖后腿.所以这里侧重的是手机里面的表现. 先来看看表 ...
- js的动态加载、缓存、更新以及复用(二)
上一篇发出来后得到了很多回复,在此首先感谢大家的热情捧场!有的推荐第三方框架,比如 In.js.requrieJS.sea.js.lab.js等.这个开阔了眼界,以前只知道sea.js,省去了自己搜索 ...
- jshint配置(js检查)
JSHint的选项配置 asi 如果是真,JSHint会无视没有加分号的行尾,自动补全分号一直是Javascript很有争议的一个语法特性.默认,JSHint会要求你在每个语句后面加上分号,但是如果你 ...
- GifShot - 创建动态 GIF 的 JavaScript 库
GifShot 是一个可以创建流媒体,视频或图像的 GIF 动画的 JavaScript 库.该库的客户端特性使其非常便携,易于集成到几乎任何网站.利用最先进的浏览器 API ,包括 WebRTC , ...
- requirejs:杏仁的优化(almond)
这里只是调侃一下,“杏仁”其实指的是almond,requirejs作者的另一个开源项目,它的定位是作为requirejs的一个替代品. 本文概要: 1. 使用场景 2. 打包例子:未使用almond ...
- web基础
1.认识webapp程序? 请求方式不同:基于事件触发------基于http协议下的http请求和http响应.点击百度一下-----发送了请求:不仅会携带问题,ip地址,主机号.请求是客户 ...
- sharepoint2010问卷调查(2)-实现问卷的图片调查(采用自定义字段类型)
1. 首先建立个图片库上传图片 并建立文件夹1和2,1下有1.1文件夹,2下2.1文件夹,2.1下有文件夹2.1.1. 在1文件夹下放如下图片: 2.建立自定义字段类型,如下图: 3.部署后建立栏目的 ...
- ora-00119和ora-00132解决方案
win7 64位 oracle 11g 先登录到sqlplus: sqlplus /nolog; 登录数据库: conn system/manager as sysdba; 然后启动数据库: ...