From a16f92ff3bc9adfc7ff9b0e2847d67c0f59b4fcc Mon Sep 17 00:00:00 2001 From: zhn <947514737@qq.com> Date: Thu, 9 Jun 2022 19:26:44 +0800 Subject: [PATCH] =?UTF-8?q?pdf=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/document/utils/ReadPdfUtil.java | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/utils/ReadPdfUtil.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/utils/ReadPdfUtil.java index 26d68e895..d961900fe 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/utils/ReadPdfUtil.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/utils/ReadPdfUtil.java @@ -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("文件内容读取失败"); +// } +// } }