zoukankan      html  css  js  c++  java
  • Java读取word文件,字体,颜色

    在Android读取Word文件时,在网上查看时可以用tm-extractors,但好像没有提到怎么读取Word文档中字体的颜色,字体,上下标等相关的属性。但由于需要,要把doc文档中的内容(字体,下划线,颜色等)读取应用到android中(不包括图片和图表)。

    后面采用的是poi三方jar包(原包太大,可以从源代码里自己抽取有用的一些代码减少包的大小)。

    我的想法是:把doc中的内容解析出来后,加上html对应的标签,在android中通过Html.fromHtml在TextView中进行显示,或者通过WebView.loadData进行加载显示

    但测试后,发现如果加载太多内容的话,在Android中效率不行。

    效果(该图的效果是在TextView中的效果,在WebView中效果会更好些):

    doc图:

    android图:

    做法1:(解析为span样式的,这种做法只能用WebView方式加载,Html.fromHtml无效)

    Java代码  收藏代码
    1. /**Span样式 
    2.      * 通过字体的样式进行加载 
    3.      * @param inputStream 
    4.      * @return 
    5.      */  
    6.     public static String readDocToSpanByRun(InputStream inputStream) {  
    7.         HWPFDocument hwpfDocument = null;  
    8.         if(inputStream == null)  
    9.             throw new RuntimeException("inputStream is null ...");  
    10.         try{  
    11.             hwpfDocument = new HWPFDocument(inputStream);  
    12.         }catch(Exception e) {  
    13.             throw new RuntimeException("HWPFDocment Exception", e);  
    14.         }  
    15.         Range allRange = hwpfDocument.getRange();  
    16.         int length = allRange.numCharacterRuns();  
    17.         StringBuffer sb = new StringBuffer();  
    18.         CharacterRun cur;  
    19.         String text = "";  
    20.         for (int i = 0; i < length; i++) {  
    21.             cur = allRange.getCharacterRun(i);  
    22.             sb.append(CharacterRunUtils.toSpanType(cur));  
    23.             text = CharacterRunUtils.getSpicalSysbomByRun(cur.text());  
    24.             if(cur.getSubSuperScriptIndex() == 1)  
    25.                 sb.append("<sup>").append(text).append("</sup>");  
    26.             else if(cur.getSubSuperScriptIndex() == 2)   
    27.                 sb.append("<sub>").append(text).append("</sub>");  
    28.             else   
    29.                 sb.append(text);  
    30.             sb.append("</span>");  
    31.         }  
    32.         return sb.toString();  
    33.     }  
    34.       

    做法2:(解析为font样式的,Html.fromHtml有效,但对应size的设置无效果)

    Java代码  收藏代码
    1. /** 
    2.      * Html样式 
    3.      * 通过字体样式解析 
    4.      * @param inputStream 
    5.      * @return 
    6.      */  
    7.     public static String readDocToHtml(InputStream inputStream) {  
    8.         HWPFDocument hwpfDocument = null;  
    9.         if(inputStream == null)  
    10.             throw new RuntimeException("inputStream is null ...");  
    11.         try{  
    12.             hwpfDocument = new HWPFDocument(inputStream);  
    13.         }catch(Exception e) {  
    14.             throw new RuntimeException("HWPFDocment Exception", e);  
    15.         }  
    16.         CharacterRun  cur = null;  
    17.         StringBuffer sb = new StringBuffer();  
    18.         StringBuffer charStr =  new StringBuffer();  
    19.         Range allRange = hwpfDocument.getRange();  
    20.         for(int i = 0; i < allRange.numCharacterRuns(); i++) {  
    21.             cur = allRange.getCharacterRun(i);  
    22.             sb.append(CharacterRunUtils.fontFaceColorSizeToHtml(cur));  
    23.             charStr.append(CharacterRunUtils.toSupOrSub(cur, CharacterRunUtils.getSpicalSysbomByRun(cur.text())));  
    24.             if(cur.isBold())  {  
    25.                 charStr.insert(0, "<b>");  
    26.                 charStr.insert(charStr.length(), "</b>");  
    27.             }  
    28.             if(cur.getUnderlineCode() != 0) {   
    29.                 charStr.insert(0, "<u>");  
    30.                 charStr.insert(charStr.length(), "</u>");  
    31.             }  
    32.             if(cur.isItalic()) {  
    33.                 charStr.insert(0, "<i>");  
    34.                 charStr.insert(charStr.length(), "</i>");  
    35.             }  
    36.             if(cur.isStrikeThrough()) {  
    37.                 charStr.insert(0, "<s>");  
    38.                 charStr.insert(charStr.length(), "</s>");  
    39.             }  
    40.             sb.append(charStr).append("</font>");  
    41.             charStr.setLength(0);  
    42.         }  
    43.         hwpfDocument = null;  
    44.         return sb.toString();  
    45.     }  

     以下是会用到的方法:

    Java代码  收藏代码
    1. /** 
    2.  *处理字体相关的属性  
    3.  */  
    4. public class CharacterRunUtils {  
    5.   
    6.     private static final short ENTER_ASCII = 13;  
    7.     private static final short SPACE_ASCII = 32;  
    8.     private static final short TABULATION_ASCII = 9;  
    9.   
    10.     /** 
    11.      * 比对字体是否相同 
    12.      * 可以继续加其它属性 
    13.      * @param cr1 
    14.      * @param cr2 
    15.      * @return 
    16.      */  
    17.     public static boolean compareCharStyleForSpan(CharacterRun cr1,  
    18.             CharacterRun cr2) {  
    19.         return cr1.isBold() == cr2.isBold()  
    20.                 && cr1.getFontName().equals(cr2.getFontName())  
    21.                 && cr1.getFontSize() == cr2.getFontSize()  
    22.                 && cr1.isItalic() == cr2.isItalic()  
    23.                 && cr1.getColor() == cr2.getColor()  
    24.                 && cr1.getUnderlineCode() == cr2.getUnderlineCode()  
    25.                 && cr1.isStrikeThrough() == cr2.isStrikeThrough()  
    26.                 && cr1.getColor() == cr2.getColor();  
    27.     }  
    28.   
    29.     public static boolean compareCharColor(CharacterRun cr1, CharacterRun cr2) {  
    30.         return cr1.getFontName().equals(cr2.getFontName())  
    31.                 && cr1.getFontSize() == cr2.getFontSize()  
    32.                 && cr1.getColor() == cr2.getColor();  
    33.     }  
    34.   
    35.     public static String getSpicalSysbom(char currentChar) {  
    36.         String tempStr = "";  
    37.         if (currentChar == ENTER_ASCII) {  
    38.             tempStr += "<br/>";  
    39.         } else if (currentChar == SPACE_ASCII) {  
    40.             tempStr += "&nbsp;";  
    41.         } else if (currentChar == TABULATION_ASCII) {  
    42.             tempStr += "&nbsp;&nbsp;&nbsp;";  
    43.         } else {  
    44.             tempStr += currentChar;  
    45.         }  
    46.         return tempStr;  
    47.     }  
    48.       
    49.     public static String getSpicalSysbomSpan(char currentChar) {  
    50.         String tempStr = "";  
    51.         if (currentChar == ENTER_ASCII) {  
    52.             tempStr += "<br/>";  
    53.         } else if (currentChar == SPACE_ASCII) {  
    54.             tempStr += "&nbsp;";  
    55.         } else if (currentChar == TABULATION_ASCII) {  
    56.             tempStr += "&nbsp;&nbsp;&nbsp;";  
    57.         }  
    58.         return tempStr;  
    59.     }  
    60.   
    61.     /** 
    62.      * 特殊字符的取代 
    63.      * @param currentChar 
    64.      * @return 
    65.      */  
    66.     public static String getSpicalSysbomByRun(String currentChar) {  
    67.         StringBuffer tempStr = new StringBuffer();  
    68.         int length = currentChar.length();  
    69.         for (int i = 0; i < length; i++) {  
    70.             tempStr.append(getSpicalSysbom(currentChar.charAt(i)));  
    71.         }  
    72.         return tempStr.toString();  
    73.     }  
    74.   
    75.     /** 
    76.      * span方式前缀 
    77.      * @param cr 
    78.      * @return 
    79.      */  
    80.     public static String toSpanType(CharacterRun cr) {  
    81.         StringBuffer spanStyle = new StringBuffer("<span style='font-family:");  
    82.         spanStyle.append(cr.getFontName()).append("; font-size:")  
    83.                 .append(cr.getFontSize() / 2).append("pt;");  
    84.         if (cr.isBold())  
    85.             spanStyle.append("font-weight:bold;");  
    86.         if (cr.isItalic())  
    87.             spanStyle.append("font-style:italic;");  
    88.         if (cr.isStrikeThrough())  
    89.             spanStyle.append("text-decoration:line-through;");  
    90.         if (cr.getUnderlineCode() != 0)  
    91.             spanStyle.append("text-decoration:underline;");  
    92.         spanStyle.append("color:")  
    93.                 .append(ColorUtils.getHexColor(cr.getIco24())).append(";")  
    94.                 .append("'>");  
    95.         return spanStyle.toString();  
    96.     }  
    97.   
    98.     /** 
    99.      * 为font方式提供<font前缀 
    100.      * @param cr 
    101.      * @return 
    102.      */  
    103.     public static String fontFaceColorSizeToHtml(CharacterRun cr) {  
    104.         StringBuffer htmlType = new StringBuffer("<font ");  
    105.         htmlType.append("size='").append(cr.getFontSize() / 2).append("' ")  
    106.                 .append("face='").append(cr.getFontName()).append("' ")  
    107.                 .append("color='")  
    108.                 .append(ColorUtils.getHexColor(cr.getIco24())).append("'>");  
    109.         return htmlType.toString();  
    110.     }  
    111.   
    112.     /** 
    113.      * 处理上下标 
    114.      * @param cr 
    115.      * @param currentChar 
    116.      * @return 
    117.      */  
    118.     public static String toSupOrSub(CharacterRun cr, String currentChar) {  
    119.         int sub = cr.getSubSuperScriptIndex();  
    120.         if (sub != 0) {  
    121.             if (sub == 1)  
    122.                 // 上标  
    123.                 return "<sup>" + currentChar + "</sup>";  
    124.             else  
    125.                 // 下标  
    126.                 return "<sub>" + currentChar + "</sub>";  
    127.         } else  
    128.             return currentChar;  
    129.     }  
    130.   
    131.     public static String toSupOrSub(CharacterRun cr, char currentChar) {  
    132.         return toSupOrSub(cr, new String(new char[]{currentChar}));  
    133.     }  
    134. }  

    用到的颜色的转换(进行简单的颜色转换)

    Java代码  收藏代码
    1. public class ColorUtils {  
    2.   
    3.     public static int  red(int c) {  
    4.         return c & 0XFF;  
    5.     }  
    6.       
    7.     public static int green(int c) {  
    8.         return (c >> 8) & 0XFF;  
    9.     }  
    10.       
    11.     public static int blue(int c) {  
    12.         return (c >> 16) & 0XFF;  
    13.     }  
    14.       
    15.     public static int rgb(int c) {  
    16.         return (red(c) << 16) | (green(c) <<8) | blue(c);  
    17.     }  
    18.   
    19.     public static String rgbToSix(String rgb) {  
    20.         int length = 6 - rgb.length();  
    21.         String str = "";  
    22.         while(length > 0){  
    23.             str += "0";  
    24.             length--;  
    25.         }  
    26.         return str + rgb;  
    27.     }  
    28.       
    29.     public static String getHexColor(int color) {  
    30.         color = color == -1 ? 0 : color;  
    31.         int rgb = rgb(color);  
    32.         return "#" + rgbToSix(Integer.toHexString(rgb));  
    33.     }  
    34. }  
  • 相关阅读:
    你所能用到的数据结构(一)
    你所能用到的数据结构(八)
    你所能用到的数据结构(六)
    你所能用到的数据结构(三)
    你所能用到的数据结构(四)
    你所能用到的无损压缩编码(二)
    你所能用到的数据结构(二)
    你所能用到的数据结构(五)
    Attribute在.NET编程中的应用(四)
    对线程安全理解的例子
  • 原文地址:https://www.cnblogs.com/newcaoguo/p/6231175.html
Copyright © 2011-2022 走看看