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); //上标

原文地址:https://www.cnblogs.com/digdeep/p/5395053.html