Document table and paragraph with POI
807580Apr 7 2009 — edited Jun 2 2010Hi wrotten the following program in order to read paragraph and tables
in a word document:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package prezziarioreader;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.model.ListTables;
//import org.apache.poi.hwpf.usermodel.CharacterRun;
import java.io.*;
/**
*
* @author Enrico
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
HWPFDocument doc = new HWPFDocument(new FileInputStream("d:\\prova.doc"));
Range r = doc.getRange ();
int sectionLevel = 0;
int lenParagraph = r.numParagraphs ();
boolean inCode = false;
String text;
Paragraph p;
for (int x = 0; x < lenParagraph; x++){
p = r.getParagraph (x);
if (p.isInTable()){
Table tab = r.getTable(p);
System.out.println("Paragraph " + x + ":"+"Table with " tab.numRows() "rows.");
}
else{
text = p.text ();
System.out.println("Paragraph " + x + ":" + text);
}
}
}
catch (IOException e){
System.err.println(e);
}
}
}
I got a problem during the execution; It read well the simple paragraphs but
when there's a table i got the following error message:
Exception in
thread "main" java.lang.IllegalArgumentException: This paragraph is not
the first one in the table
at org.apache.poi.hwpf.usermodel.
Range.getTable(Range.java:873)
What's worng?