pdf解析

This commit is contained in:
zhn
2022-06-09 19:26:44 +08:00
parent 8214b38b07
commit a16f92ff3b
@@ -1,13 +1,13 @@
package com.jero.modules.document.utils;
import com.jero.common.exception.JeroBootException;
import com.jero.common.util.oss.CosBootUtil;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* @description
@@ -17,24 +17,48 @@ import java.io.IOException;
public class ReadPdfUtil {
public static String readPdf(String path) {
// path="E:\\DeskTop\\4444.pdf";
try (PDDocument document = PDDocument.load(new File(path))) {
document.getClass();
if(CosBootUtil.doesObjectExist(path)){
InputStream in = CosBootUtil.download(path);
try (PDDocument document = PDDocument.load(in)) {
document.getClass();
// if(!document.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper tStripper = new PDFTextStripper();
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(document);
return pdfFileInText;
String pdfFileInText = tStripper.getText(document);
return pdfFileInText;
// String[] lines = pdfFileInText.split("\\r?\\n");
// for(String line : lines) {
// System.out.println(line);
// }
// }
}catch (IOException e) {
throw new JeroBootException("文件内容读取失败");
}catch (IOException e) {
throw new JeroBootException("文件内容读取失败");
}
}
return null;
}
// public static String readPdf(String path) {
//// path="E:\\DeskTop\\4444.pdf";
// try (PDDocument document = PDDocument.load(new File(path))) {
// document.getClass();
//// if(!document.isEncrypted()) {
// PDFTextStripperByArea stripper = new PDFTextStripperByArea();
// stripper.setSortByPosition(true);
// PDFTextStripper tStripper = new PDFTextStripper();
//
// String pdfFileInText = tStripper.getText(document);
// return pdfFileInText;
//
//// String[] lines = pdfFileInText.split("\\r?\\n");
//// for(String line : lines) {
//// System.out.println(line);
//// }
//// }
// }catch (IOException e) {
// throw new JeroBootException("文件内容读取失败");
// }
// }
}