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
          getSubOrSupChunksString 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 nextSubOrSupTagString input) {
		int supIdx = StringUtils.indexOfinput, Constants.SUPERSCRIPT);
		int subIdx = StringUtils.indexOfinput, 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 nextEndTagString tag) {
		if Constants.SUPERSCRIPT.equalsIgnoreCasetag)) {
			return Constants.SUPERSCRIPT_END;
		} else {
			return Constants.SUBSCRIPT_END;
		}
	}
 
        public Phrase getSubOrSupChunksString temp, Font textFt, Font textriseFt) {
		Phrase phrase = new Phrase);
		String nextTag = nextSubOrSupTagtemp);
		String endTag = nextEndTagnextTag);
		int tagCount = StringUtils.countMatchestemp, Constants.SUBSCRIPT) + StringUtils.countMatchestemp, Constants.SUPERSCRIPT);
		for int i=0;i<tagCount;i++) {
			logger.debug"i i);
			if i == 0) {
				phrase.addnew ChunkStringUtils.substringBeforetemp, nextTag), textFt));
			} else {
				temp = StringUtils.substringAftertemp, nextTag);
				nextTag = nextSubOrSupTagtemp);
				phrase.addnew ChunkStringUtils.substringBetweentemp, endTag, nextTag), textFt));
				endTag = nextEndTagnextTag);
			}
			if Constants.SUBSCRIPT.equalsIgnoreCasenextTag)) {
			    phrase.addnew ChunkStringUtils.substringBetweentemp, nextTag, endTag), textriseFt).setTextRise-3f));
			} else {
			    phrase.addnew ChunkStringUtils.substringBetweentemp, nextTag, endTag), textriseFt).setTextRise5f));
			}
			if i == tagCount -1) {
				temp = StringUtils.substringAftertemp, nextTag);
				phrase.addnew ChunkStringUtils.substringAftertemp, 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.getLoggerPdfUtil.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 nextSubOrSupTagString input) { int supIdx = StringUtils.indexOfinput, SUPERSCRIPT); int subIdx = StringUtils.indexOfinput, 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 nextEndTagString tag) { if SUPERSCRIPT.equalsIgnoreCasetag)) { return SUPERSCRIPT_END; } else { return SUBSCRIPT_END; } } public static Phrase getSubOrSupChunksString temp, Font textFt, Font textriseFt) { Phrase phrase = new Phrase); String nextTag = nextSubOrSupTagtemp); String endTag = nextEndTagnextTag); int tagCount = StringUtils.countMatchestemp, SUBSCRIPT) + StringUtils.countMatchestemp, SUPERSCRIPT); for int i=0;i<tagCount;i++) { logger.debug"i:" + i); if i == 0) { phrase.addnew ChunkStringUtils.substringBeforetemp, nextTag), textFt)); } else { temp = StringUtils.substringAftertemp, nextTag); nextTag = nextSubOrSupTagtemp); phrase.addnew ChunkStringUtils.substringBetweentemp, endTag, nextTag), textFt)); endTag = nextEndTagnextTag); } if SUBSCRIPT.equalsIgnoreCasenextTag)) { phrase.addnew ChunkStringUtils.substringBetweentemp, nextTag, endTag), textriseFt).setTextRise-3f)); } else { phrase.addnew ChunkStringUtils.substringBetweentemp, nextTag, endTag), textriseFt).setTextRise5f)); } if i == tagCount -1) { temp = StringUtils.substringAftertemp, nextTag); phrase.addnew ChunkStringUtils.substringAftertemp, endTag), textFt)); } } return phrase; } }

使用方法:

		Phrase phrase = PdfUtil.getSubOrSupChunksj62.getString"j6202"),fontGeneral,fontGeneral);
		p = new Paragraphphrase);
		p.setAlignmentElement.ALIGN_LEFT);
		cellC.addElementp);
		cellC.setColspan3);
		tableContent.addCellcellC);

将含有 数字科学计数法 有上标 或者小标的 字符串经过 PdfUtil.getSubOrSupChunks) 处理之后,得到一个 Phrase,然后使用它初始化得到一个 Paragraph对象,就可以加入到 表格中了。 

原理是调用:

new Chunkxxx, textriseFt).setTextRise-3f); //小标
new Chunkxxx), textriseFt).setTextRise5f); //上标

Published by

风君子

独自遨游何稽首 揭天掀地慰生平