Getting java.lang.IllegalArgumentException: The end (7905) must not be before the start (15721) Exception while reading word document

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 21:07:44

问题


I am parsing Microsoft Word documents. I have imported Apache poi jar to read the Word document. I want to get the headings present in the Word document. I have given the size of the headings to get that filtered.

public void try1(POIFSFileSystem filestream) throws Exception
{
    HWPFDocument doc = new HWPFDocument (filestream);
    WordExtractor we = new WordExtractor(doc);
    Range range = doc.getRange();
    String[] paragraphs = we.getParagraphText();
    for (int i = 0; i < paragraphs.length; i++)
    {
      Paragraph pr = range.getParagraph(i);
      int k = 0;
      if(pr.text().trim().length()!=0)
      {
         while (true) 
         {

             System.out.println(k);
             CharacterRun run = pr.getCharacterRun(k++);
             /*System.out.println("Word is "+pr.text());
             System.out.println("Color: " + run.getColor());
             System.out.println("Font: " + run.getFontName());
             System.out.println("Font Size: " + run.getFontSize());*/
             System.out.println(pr.text());
             System.out.println(run.getEndOffset()+"  "+pr.getEndOffset());
             if(run.getFontSize()==26||run.getFontSize()==24)
             {
                 System.out.println("Selected One is "+pr.text());
             }
             if (run.getEndOffset() == pr.getEndOffset())
                 break;
         }
      }
    }
}

am getting this exception :

java.lang.IllegalArgumentException: The end (7905) must not be before the start (15721)
at org.apache.poi.hwpf.usermodel.Range.sanityCheckStartEnd(Range.java:247)
at org.apache.poi.hwpf.usermodel.Range.<init>(Range.java:181)
at org.apache.poi.hwpf.usermodel.CharacterRun.<init>(CharacterRun.java:98)
at org.apache.poi.hwpf.usermodel.Range.getCharacterRun(Range.java:791)
at com.honeywell.corept.srd.ReadDocFileFromJava.try1(ReadDocFileFromJava.java:122)
at com.honeywell.corept.srd.ReadDocFileFromJava.main(ReadDocFileFromJava.java:24)

CharacterRun run = pr.getCharacterRun(k++); this is 122 line in the java file

来源:https://stackoverflow.com/questions/24529418/getting-java-lang-illegalargumentexception-the-end-7905-must-not-be-before-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!