zoukankan      html  css  js  c++  java
  • 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); //上标

  • 相关阅读:
    Windows Phone 8 Wallet 手机钱包 / 电子钱包
    Windows Phone 8 In app purchase 应用内购买 / 应用内支付
    Windows Phone 8 适应多屏分辨率
    Windows phone 8 基于定位的后台应用
    Windows Phone 8 Nokia地图控件
    Windows Phone 8 MDIL编译与代码混淆工具
    Windows Phone 8 近场通信 NFC / Bluetooth Proximity
    Windows Phone 8 镜头应用 Lenses for Windows Phone 8
    Windows Phone 8 与 windows 8 开发技术概览
    嵌入式成长轨迹54 【Zigbee项目】【CC2430基础实验】【系统睡眠工作状态】
  • 原文地址:https://www.cnblogs.com/digdeep/p/5395053.html
Copyright © 2011-2022 走看看