2017-09-11 8 views
0

私はABBYY finereaderを使用してPDF文書を単語文書に変換しました。 XWPFTable(Apache POI)では、ドキュメントwordに存在するテーブルが認識されません。以下はXWPFTableが単語文書内の表を認識しない

表形式である:

以下
Heading1  Heading2  Heading3 Heading4 
Sub-heading1 Sub-heading2   
2011   36.66   ABC  24,000 C 
2012   46.90   ABC  78,000 C 
       ​    ABC  90,000 D 

は、コードの私の作品です:

import java.io.FileInputStream; 
import java.util.Iterator; 
import java.util.List; 
import org.apache.poi.openxml4j.opc.OPCPackage; 
import org.apache.poi.xwpf.usermodel.IBodyElement; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.apache.poi.xwpf.usermodel.XWPFTable; 

public class TableExtraction { 
    public static void main(String[] args) { 
    try { 
     FileInputStream fis = new FileInputStream("<path to docx file>"); 
     XWPFDocument xdoc=new XWPFDocument(OPCPackage.open(fis)); 
     Iterator<IBodyElement> bodyElementIterator = xdoc.getBodyElementsIterator(); 
     while(bodyElementIterator.hasNext()) { 
     IBodyElement element = bodyElementIterator.next(); 
     if("TABLE".equalsIgnoreCase(element.getElementType().name())) { 
      System.out.println("Table Data"); 
      List<XWPFTable> tableList = element.getBody().getTables(); 
      for (XWPFTable table: tableList) { 
      System.out.println("Total Number of Rows of Table:" + table.getNumberOfRows()); 
      System.out.println(table.getText()); 
      } 
     } 
     else { 
      System.out.println("Not a Table Data"); 
     } 
     } 
     xdoc.close(); 
    } 
    catch(Exception ex) { 
     ex.printStackTrace(); 
    } 
    } 
} 

は出力:

ない表のデータ

+0

私はそれが実際にはテーブルではなく、タブで区切られたテキストを含む段落と思われます。たとえば、最初の行は "Heading1 \ tHeading2 \ tHeading3 \ tHeading4"です。 'Word'を使って文書を開くと、それが表示されます。 –

答えて

0

私はそれを使って試しましたあなたのコードを私のWordテーブルに入れておけば、うまくいきませんでした。それは通常のWordの表であると仮定すると、あなたが直接このようなテーブルを反復処理することができます。

public static void main(String[] args) throws IOException { 
    FileInputStream fis = new FileInputStream(FILE_NAME); 
    XWPFDocument xdoc = new XWPFDocument(fis); 

    for (XWPFTable table : xdoc.getTables()) { 
     System.out.println(table.getRows().size()); 

      //in case you want to do more with the table cells... 
     for (XWPFTableRow row : table.getRows()) { 
      for (XWPFTableCell cell : row.getTableCells()) { 
       for (XWPFParagraph para : cell.getParagraphs()) { 
        System.out.println(para.getText()); 
       } 
      } 
     } 
    } 
    fis.close(); 
    xdoc.close(); 
} 

を、これはどちらか、動作しない場合は、何かがおそらくPDFからの変換で間違ってしまいました。

関連する問題