使用java将pdf表格数据转换为excel
我想将我的pdf表格文件转换为CSV文件 这是我写的代码,但我只得到5列名称,而不是它们的值 代码--使用java将pdf表格数据转换为excel,java,csv,pdf,pdfbox,Java,Csv,Pdf,Pdfbox,我想将我的pdf表格文件转换为CSV文件 这是我写的代码,但我只得到5列名称,而不是它们的值 代码-- publicstaticarraylistreadparafromfrompdf(字符串pdfPath、int-pageNoStart、int-pageNoEnd、int-noOfColumnsInTable){ ArrayList objArrayList=新的ArrayList(); 试一试{ PDDocument document=PDDocument.load(新文件(pdfPath)
publicstaticarraylistreadparafromfrompdf(字符串pdfPath、int-pageNoStart、int-pageNoEnd、int-noOfColumnsInTable){
ArrayList objArrayList=新的ArrayList();
试一试{
PDDocument document=PDDocument.load(新文件(pdfPath));
document.getClass();
如果(!document.isEncrypted()){
PDFTextStripperByArea剥离器=新的PDFTextStripperByArea();
脱扣器。设置端口BYPOSITION(真);
PDFTextStripper tStripper=新的PDFTextStripper();
tStripper.setStartPage(页面开始);
tStripper.setEndPage(pageNoEnd);
字符串pdfFileInText=tStripper.getText(文档);
//按空格分割
字符串Documentlines[]=pdfFileInText.split(\\r?\\n”);
用于(字符串行:文档行){
字符串lineArr[]=line.split(\\s+);
if(lineArr.length==noOfColumnsInTable){
用于(字符串行数据:lineArr){
系统输出打印(linedata+“”);
}
System.out.println(“”);
objArrayList.add(lineArr);
}
}
}
}捕获(例外e){
系统输出打印项次(“例外”+e);
}
返回对象列表;
}
添加更多标签以更好地搜索如果您可以发布pdfFileInText
的值,我们将看到您正在使用的内容,这将非常有帮助。pdfFileInText
是否确实有您要查找的数据?请共享有问题的pdf。观察到您只获取标题而不获取内容,这可能表明内容不是作为页面内容存储的,而是作为表单字段或其他注释内容存储的。在这种情况下,您必须以不同的方式提取数据。
public static ArrayList<String[]> readParaFromPDF(String pdfPath, int pageNoStart, int pageNoEnd, int noOfColumnsInTable) {
ArrayList<String[]> objArrayList = new ArrayList<>();
try {
PDDocument document = PDDocument.load(new File(pdfPath));
document.getClass();
if (!document.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper tStripper = new PDFTextStripper();
tStripper.setStartPage(pageNoStart);
tStripper.setEndPage(pageNoEnd);
String pdfFileInText = tStripper.getText(document);
// split by whitespace
String Documentlines[] = pdfFileInText.split("\\r?\\n");
for (String line : Documentlines) {
String lineArr[] = line.split("\\s+");
if (lineArr.length == noOfColumnsInTable) {
for (String linedata : lineArr) {
System.out.print(linedata + " ");
}
System.out.println("");
objArrayList.add(lineArr);
}
}
}
} catch (Exception e) {
System.out.println("Exception " + e);
}
return objArrayList;
}