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如何存储特殊字符、上标、下标
测试验证特殊符号能否存入数据库中: 其中,像一些普通单位符号比如“ ° ′"﹩ $ ﹠ & £¥ ‰ % ℃ ¤ ¢℉”可以正常录入没有问题,但是万分号“‱”之上就不可以了,录入后显 ...
随机推荐
- 什么是CGI
什么是CGI 1. 定义: CGI(Common Gateway Interface)是HTTP服务器与你的或其它机器 上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. 2. 功能: 绝大 ...
- JavaMail API 详细分解
在使用Spring框架的过程中,它的优势之一就是在于跟其他一些技术的整合,如JavaMail .任务调度.缓存策略等技术.今天就Java Mail详细阐述.JavaMail API是被设计为与协议无关 ...
- Scalaz(21)-类型例证:Liskov and Leibniz - type evidence
Leskov,Leibniz,别扭的名字,是什么地干活?碰巧从scalaz源代码里发现了这么个东西:scalaz/BindSyntax.scala /** Wraps a value `self` a ...
- 【Effective Java】11、同步访问共享的可变数据
这段时间看的部分感觉没啥需要记录下来的,个人也没什么想法,不过以后还是要多记,多写 package cn.xf.cp.ch02.item66; import java.util.concurrent. ...
- jQuery Mobile页面返回无需重新get
最近公司的web app项目,使得我有幸一直接触和学习jQuery Mobile.这确实是一个很不错的移动开发库,有助于擅长web开发的工程师,快速入门并构建自己的移动应用.但是在前两天,我碰到了一个 ...
- rabbitmq binary/other_system内存占用很高
最近有台服务器的MQ应用占用内存比较偏高,如下: 但是看控制台本身内存中消息积压并不多, 查看rabbtmqctl发现,binary data和other data占据了绝大部分的内存,如下: {me ...
- Weex 环境搭建(win7)
安装 Node.js node.js需要4.0+ 百度云下载地址http://pan.baidu.com/s/1o84g6c6 官网下载地址https://nodejs.org/en/ 安装教程请看这 ...
- Ubuntu 各版本代号简介
起名字是件伤脑筋的事,但是程序猿们似乎最喜欢干伤脑筋的活.Android 的每个版本都有个甜点的别名,而 Ubuntu ,每个版本都有一个更为特色的名字,这个名字由一个形容词和一个动物名称组成,并且, ...
- JavaScript学习笔记6 之经典神坑题整理
本篇内容是关于容易出错题的整理,这些题也有利于对javascript的语法逻辑的理解,分析的内容仅供参考: <script> */ var x=0 , y=0 , c=1; functio ...
- js动态计算移动端rem
在做移动端web app的时候,众所周知,移动设备分辨率五花八门,虽然我们可以通过CSS3的media query来实现适配,例如下面这样: html { font-size : 20px; } @m ...