• 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也是正常的

  • 相关阅读:
    《演讲之禅》助你成长为一名合格程序员
    JavaScript中设置元素class的三种方法小结
    数据类型和Json格式
    JavaScript给onclick事件赋值,动态传参数实例辩错
    JSON.parse()和JSON.stringify()使用
    javascript写在<head>和<body>里的区别
    Source Insight 相关
    串口接收字符时,最后一个字符延时发送
    嵌入式编程中调试问题全记录
    新版数采仪开发问题全记录
  • 原文地址:https://www.cnblogs.com/bing521meng/p/6536704.html
Copyright © 2020-2023  润新知