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如何存储特殊字符、上标、下标
测试验证特殊符号能否存入数据库中: 其中,像一些普通单位符号比如“ ° ′"﹩ $ ﹠ & £¥ ‰ % ℃ ¤ ¢℉”可以正常录入没有问题,但是万分号“‱”之上就不可以了,录入后显 ...
随机推荐
- 框架SpringMVC笔记系列 一 基础
主题:SpringMVC 学习资料参考网址: 1.http://www.icoolxue.com 2.http://aokunsang.iteye.com/blog/1279322 1.SpringM ...
- php中读写excel表格文件示例。
测试环境:php5.6.24.这块没啥兼容问题. 需要更多栗子,请看PHPExcel的examples.还是蛮强大的. 读取excel文件. 第一步.下载开源的PHPExcel的类库文件,官方网站是h ...
- quickstart.sh
#!/bin/bashjava_pid=`ps -ef | grep java | grep 'com.kzhong.huamu.sisyphus.QuickStartServer' | awk '{ ...
- 高性能 Windows Socket 组件 HP-Socket v3.0.1 正式发布
HP-Socket 是一套通用的高性能 Windows Socket 组件包,包含服务端组件(IOCP 模型)和客户端组件(Event Select 模型),广泛适用于 Windows 平台的 TCP ...
- Tab切换类型
Tab切换类型 点击Tab 滑过Tab 延迟Tab CSS样式 ; ; list-style:none; font-size:12px;} .notice{width:298px; height:98 ...
- ASP.NET登录控件login。
1.Login控件.通常情况下会出现3个核心元素.用户名文本框.密码输入框.提交凭证的按钮. 1>.比较重要的属性:CreateUserText属性:包含站点注册页的链接文本.CreateUse ...
- Resumable.js – 基于 HTML5 File API 的文件上传
Resumable.js 是一个 JavaScript 库,通过 HTML5 文件 API 提供,稳定和可恢复的批量上传功能.在上传大文件的时候通过每个文件分割成小块,每块在上传失败的时候,上传会不断 ...
- 定制Eclipse IDE之插件篇(二)
上文回顾:定制Eclipse IDE之插件篇(一) 延续上一篇的插件篇,这一篇将会讲到一个最关键的插件aptana. 一.aptana插件 官方的解释我就不说了,从下面图可以看到插件提供了什么功能,列 ...
- JavaScript学习笔记-函数
函数的两种创建方式:函数定义表达式.函数声明语句 编译时,函数声明语句创建的函数会‘被提前’至外部函数的作用域顶部,在该作用域内可以被随意调用: 而函数表达式创建的函数,要调用它必须赋值给一个变量,编 ...
- js判断radiobuttonlist的选中值显示/隐藏其它模块
<script> $(function () { var SelectVal = $("input[name='rblGJS']:checked").val(); if ...