0

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

takendarkk
  • 3,347
  • 8
  • 25
  • 37
user2572003
  • 743
  • 3
  • 8
  • 16

0 Answers0