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; package com.jero.modules.document.utils;
import com.jero.common.exception.JeroBootException; import com.jero.common.exception.JeroBootException;
import com.jero.common.util.oss.CosBootUtil;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea; import org.apache.pdfbox.text.PDFTextStripperByArea;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
/** /**
* @description * @description
@@ -17,24 +17,48 @@ import java.io.IOException;
public class ReadPdfUtil { public class ReadPdfUtil {
public static String readPdf(String path) { public static String readPdf(String path) {
// path="E:\\DeskTop\\4444.pdf"; if(CosBootUtil.doesObjectExist(path)){
try (PDDocument document = PDDocument.load(new File(path))) { InputStream in = CosBootUtil.download(path);
document.getClass(); try (PDDocument document = PDDocument.load(in)) {
document.getClass();
// if(!document.isEncrypted()) { // if(!document.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea(); PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true); stripper.setSortByPosition(true);
PDFTextStripper tStripper = new PDFTextStripper(); PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(document); String pdfFileInText = tStripper.getText(document);
return pdfFileInText; return pdfFileInText;
// String[] lines = pdfFileInText.split("\\r?\\n"); // String[] lines = pdfFileInText.split("\\r?\\n");
// for(String line : lines) { // for(String line : lines) {
// System.out.println(line); // System.out.println(line);
// } // }
// } // }
}catch (IOException e) { }catch (IOException e) {
throw new JeroBootException("文件内容读取失败"); 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("文件内容读取失败");
// }
// }
} }