Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        去年关闭。
                                                                                            
                
        
我正在将数据从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) {
}

最佳答案

如果您对PDF有一点了解,那将是很有意义的。
PDF不是所见即所得格式。它更多是指令的容器,而不是人类可读内容的容器。

内部的PDF文件看起来像这样


  去坐标50,50
  使用字体Helvetica Bold
  将字体大小设置为12
  绘制字符“ H”的字形
  去坐标56、50
  绘制字符“ e”的字形


话虽这么说,很难将这种非结构化数据还原回一个合理的表。

关于java - PDF to Java格式的Excel ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49726217/

10-11 02:27
查看更多