我需要从pdf文件及其标题中获取数据,以便进一步与数据库数据进行比较
我尝试使用pdfbox,google vision ocr,itext,但是所有库都给我一行没有结构和标题的行。
例如:Date \ nNumber \ nStatus \ n12 \ 12 \ 2020 \ n442334 \ delivered
我将尝试将pdf转换为excel / word并从中获取数据,但是为此,我需要读取pdf并将数据写入excel / word中
如何获得带有标题的数据?
我找不到我的问题的答案。
我将以下代码用于任务:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class ExtractTextByArea {
public String getTextFromCoordinate(String filepath,int x,int y,int width,int height) {
String result = "";
try (PDDocument document = PDDocument.load(new File(filepath))) {
if (!document.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
// Rectangle rect = new Rectangle(260, 35, 70, 10);
Rectangle rect = new Rectangle(x,y,width,height);
stripper.addRegion("class1", rect);
PDPage firstPage = document.getPage(0);
stripper.extractRegions( firstPage );
// System.out.println("Text in the area:" + rect);
result = stripper.getTextForRegion("class1");
}
} catch (IOException e){
System.err.println("Exception while trying to read pdf document - " + e);
}
return result;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句