Java讀取word文件,字體,顏色

在Android讀取Word文件時,在網上查看時能夠用tm-extractors,但好像沒有提到怎麼讀取Word文檔中字體的顏色,字體,上下標等相關的屬性。但因爲須要,要把doc文檔中的內容(字體,下劃線,顏色等)讀取應用到android中(不包括圖片和圖表)。html

後面採用的是poi三方jar包(原包太大,能夠從源代碼裏本身抽取有用的一些代碼減小包的大小)。android

個人想法是:把doc中的內容解析出來後,加上html對應的標籤,在android中經過Html.fromHtml在TextView中進行顯示,或者經過WebView.loadData進行加載顯示app

但測試後,發現若是加載太多內容的話,在Android中效率不行。測試

 

效果(該圖的效果是在TextView中的效果,在WebView中效果會更好些):字體

doc圖:spa

 

 

android圖:htm

 

 

作法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的設置無效果)ip

 

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. }  
相關文章
相關標籤/搜索