rtf乱码解决办法
首先,阐述下rtf,富文本格式文档,目前常用来做模板;
我遇到的问题是rtf中替换后的文本显示是正常的,rtf直接转pdf就不正常了,通过notpad++ 打开后发现rtf本身内容编码是我没有见过的(查资料说时ansi,具体我也不晓得了),我替换后的则是中文显示,这样转换后中文全部乱码;
解决办法如下,直接贴util类分享
package com.ywlqi.util; public class StringToRtfUtil{ public static String chineseStrTortf(String content){ StringBuffer sb = new StringBuffer(""); if (content == null) return null; for (char c : content.toCharArray()) { if(isChinese(c)){ System.out.println(c); sb.append(strToRtf(String.valueOf(c))); }else{ sb.append(c); } } return sb.toString(); } public static void main(String[] args) { System.out.println(chineseStrTortf("1)在合同有效期内,乙方为甲方提供简历库下载的用户名与初始密码")); } public static byte[] charToByte(char c) { byte[] b = new byte[2]; b[0] = (byte) ((c & 0xFF00) >> 8); b[1] = (byte) (c & 0xFF); return b; } public static boolean isChinese(char c) { // return c >= 0x4E00 && c <= 0x9FA5;// 根据字节码判断 Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true; } return false; } public static String strToRtf(String content) { StringBuffer sb = new StringBuffer(""); try { char[] digital = "0123456789ABCDEF".toCharArray(); byte[] bs = null; bs = content.getBytes("GB2312"); int bit; for (int i = 0; i < bs.length; i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append("\\'"); sb.append(digital[bit]); bit = bs[i] & 0x0f; sb.append(digital[bit]); } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } public static String byteToRtf(byte[] bs) { StringBuffer sb = new StringBuffer(""); try { char[] digital = "0123456789ABCDEF".toCharArray(); int bit; for (int i = 0; i < bs.length; i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append("\\'"); sb.append(digital[bit]); bit = bs[i] & 0x0f; sb.append(digital[bit]); } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } }
在模板替换的时候使用
chineseStrTortf或者strToRtf即可,两者区别在于,你是把所有字符串转成rtf编码还是仅转换中文
rtfTemplate.put("value_taxAddr", StringToRtfUtil.strToRtf(taxAddr));
这样rtf文档显示正常,pdf也是正常的