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. webservice入门(2)开发ws程序

    因为webservice分为服务端和客户端,所以如果要学习的话,那么肯定是包括这两部分的了. 1.开发服务端的webservice: 使用jdk开发ws其实很简单,只是需要一些注解:最重要的是 @We ...

  2. JDBC增删改查和查唯一的完整代码

    第一部分代码(实体类) package com.wf.entity; public class Hehe{ private int hehe_id; private String hehe_name; ...

  3. tp5页面输出时,搜索后跳转下一页的处理

    tp5页面输出时,搜索功能在跳转下一页时,如果不做任何处理,会返回原有是第二页输出的数据.为了保证跳转下一页时输出的是搜索到的数据,做以下处理. (要根据自己的搜索字段进行适当修改) 页面js代码,给 ...

  4. python2.x 默认编码问题

    python2.x中处理中文,是一件头疼的事情.网上写这方面的文章,测次不齐,而且都会有点错误,所以在这里打算自己总结一篇文章. 我也会在以后学习中,不断的修改此篇博客. 这里假设读者已有与编码相关的 ...

  5. R语言XML格式数据导入与处理

    数据解析 XML是一种可扩展标记语言,它被设计用来传输和存储数据.XML是各种应用程序之间进行数据传输的最常用的工具.它与Access,Oracle和SQL Server等数据库不同,数据库提供了更强 ...

  6. GJM : Python简单爬虫入门(二) [转载]

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  7. 从web编辑器 UEditor 中单独提取图片上传,包含多图片单图片上传以及在线涂鸦功能

    UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码.(抄的...) UEditor是非常好用的富文 ...

  8. get()和post()方法编码的区别

    在做ssh搭建的框架中,在后台条件查询的时候,组合查询传到后台的值一直是乱码,其中在form表单中必须要加上method,这一点是肯定的,但是加上了提交的方式之后,会出现如下问题: 如果使用get方法 ...

  9. 基于NXBRE规则引擎实现的柔性折扣策略

    规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.接受数据输入,解释业务规则,并根据业务规则做出业务决策.应用背景 ...

  10. Quartz.NET开源作业调度框架系列(四):Plugin Job

    如果在Quartz.NET作业运行时我们想动态修改Job和Trigger的绑定关系,同时修改一些参数那么该怎么办呢?Quartz.NET提供了插件技术,可以通过在XML文件中对Job和Trigger的 ...