用 Java 将 PDF 转为 Excel

Raj*_*Raj 2 java itext apache-poi

我正在将数据从 PDF 转换为 Excel。PDF 包含表格。我使用 Itext-pdf 读取数据,并在 apache poi 的帮助下将其转换为 excel。但所有内容都转换为文本,甚至表格行和列。我喜欢把表格写成行和列,就像在 PDF 中一样,所以请帮助我阅读 PDF 中的表格以写入 Excel。

这是我的代码:

PdfReader reader;
try {
    reader = new PdfReader("D:/JDEV_WORK/MANOJ/ItemPriceReport.pdf");
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);
    TextExtractionStrategy strategy;
    String line = null;
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        strategy = parser.processContent(i,new SimpleTextExtractionStrategy());
        line = strategy.getResultantText();
        System.out.println("line --- "+line);
    }

//conversion starts here....

HSSFRow myRow = null;
HSSFCell myCell = null;
CreationHelper helper = myWorkBook.getCreationHelper();
List<String> lines = IOUtils.readLines(new StringReader(line));

for (int i = 0; i < lines.size(); i++) {
    String str[] = lines.get(i).split(",");
    myRow = mySheet.createRow((short) i);
    for (int j = 0; j < str.length; j++) {
    myRow.createCell(j).setCellValue(helper.createRichTextString(str[j]));
   }
}



        FileOutputStream fileOut;
        try {
            fileOut = new FileOutputStream("D:/JDEV_WORK/MANOJ/ItemPriceExcel.xls");
            myWorkBook.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            System.out.println("FILE NOT FOUND");
        }
    reader.close();
    } catch (IOException e) {
}
Run Code Online (Sandbox Code Playgroud)

Jor*_*ens 5

如果您对 PDF 有一点了解,那就完全有道理了。PDF 不是所见即所得的格式。它更像是一个指令容器,而不是人类可读内容的容器。

在内部,PDF 文件看起来像这样

转到坐标 50, 50
使用字体 Helvetica Bold
将字体大小设置为 12
绘制字符 'H' 的字形
转到坐标 56, 50
绘制字符 'e' 的字形

话虽如此,将这些非结构化数据恢复到合理的表是非常困难的。