2010-11-15 181 views

回答

5

我得到了解决

  HWPFDocument doc = new HWPFDocument(fs); 
      WordExtractor we = new WordExtractor(doc); 
      Range range = doc.getRange();  
      String[] paragraphs = we.getParagraphText(); 
      for (int i = 0; i < paragraphs.length; i++) { 
       org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i); 

       System.out.println(pr.getEndOffset()); 
       int j=0; 
       while (true) {    
       CharacterRun run = pr.getCharacterRun(j++); 
       System.out.println("-------------------------------");    
       System.out.println("Color---"+ run.getColor()); 
       System.out.println("getFontName---"+ run.getFontName()); 
       System.out.println("getFontSize---"+ run.getFontSize());   

       if(run.getEndOffset()==pr.getEndOffset()){ 
        break; 
       } 
       } 
} 
2

我发现它在:

CharacterRun run = para.getCharacterRun(i) 

i应该是整数,应该增加这样的代码将是如下:

int c=0; 
while (true) { 
    CharacterRun run = para.getCharacterRun(c++); 
    int x = run.getPicOffset(); 
    System.out.println("pic offset" + x); 
    if (run.getEndOffset() == para.getEndOffset()) { 
     break; 
    } 
} 
+0

要将代码格式化为代码 - 在行首添加4个空格。 – Artemix 2012-11-20 10:45:55

0
if (paragraph != null) 
      { 
       int numberOfRuns = paragraph.NumCharacterRuns; 
       for (int runIndex = 0; runIndex < numberOfRuns; runIndex++) 
       { 
        CharacterRun run = paragraph.GetCharacterRun(runIndex); 
        string color = getColor24(run.GetIco24()); 

       } 
    } 

GetColor24函数进行转换颜色的十六进制格式的C#

 public static String getColor24(int argbValue) 
    { 
     if (argbValue == -1) 
      return ""; 

     int bgrValue = argbValue & 0x00FFFFFF; 
     int rgbValue = (bgrValue & 0x0000FF) << 16 | (bgrValue & 0x00FF00) 
       | (bgrValue & 0xFF0000) >> 16; 

     StringBuilder result = new StringBuilder("#"); 
     String hex = rgbValue.ToString("X"); 
     for (int i = hex.Length; i < 6; i++) 
     { 
      result.Append('0'); 
     } 
     result.Append(hex); 
     return result.ToString(); 
    } 
0

如果您正在使用的docx(OOXML),你可能想看看这个:

import java.io.* 
import org.apache.poi.xwpf.usermodel.XWPFDocument 


fun test(){ 
    try { 
      val file = File("file.docx") 
      val fis = FileInputStream(file.absolutePath) 
      val document = XWPFDocument(fis) 
      val paragraphs = document.paragraphs 

      for (para in paragraphs) { 
       println("-- ("+para.alignment+") " + para.text) 

       para.runs.forEach { it -> 
        println(
          "text:" + it.text() + " " 
            + "(color:" + it.color 
            + ",fontFamily:" + it.fontFamily 
            + ")" 

        ) 
       } 

      } 

      fis.close() 
     } catch (e: Exception) { 
      e.printStackTrace() 
     } 
} 
相关问题