首页 > 代码库 > rtf乱码解决办法
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也是正常的
rtf乱码解决办法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。