From 6d1b6443cbfdeb4c478747d2a38d82e4a2c72449 Mon Sep 17 00:00:00 2001 From: Pavlo Glushchenko Date: Sun, 5 Nov 2023 16:31:12 -0500 Subject: [PATCH] Bump org.apache.pdfbox.pdfbox from 2.0.27 to 3.0.0 --- gradle/dependencies.gradle | 2 +- src/main/java/com/codeborne/pdftest/PDF.java | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index 5eae0e2..5bd8939 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -1,5 +1,5 @@ dependencies { - implementation group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.27', transitive: true + implementation group: 'org.apache.pdfbox', name: 'pdfbox', version: '3.0.0', transitive: true implementation group: 'org.hamcrest', name: 'hamcrest', version: '2.2', transitive: false implementation group: 'org.assertj', name: 'assertj-core', version: '3.23.1', transitive: false testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1' diff --git a/src/main/java/com/codeborne/pdftest/PDF.java b/src/main/java/com/codeborne/pdftest/PDF.java index 52e74c3..4421254 100644 --- a/src/main/java/com/codeborne/pdftest/PDF.java +++ b/src/main/java/com/codeborne/pdftest/PDF.java @@ -6,6 +6,9 @@ import com.codeborne.pdftest.matchers.DoesNotContainExactText; import com.codeborne.pdftest.matchers.DoesNotContainText; import com.codeborne.pdftest.matchers.MatchesText; +import org.apache.pdfbox.io.RandomAccessRead; +import org.apache.pdfbox.io.RandomAccessReadBuffer; +import org.apache.pdfbox.pdfparser.PDFParser; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; import org.apache.pdfbox.text.PDFTextStripper; @@ -22,7 +25,6 @@ public class PDF { public final byte[] content; - public final String text; public final int numberOfPages; public final String author; @@ -44,8 +46,8 @@ private PDF(String name, byte[] content) { private PDF(String name, byte[] content, int startPage, int endPage) { this.content = content; - try (InputStream inputStream = new ByteArrayInputStream(content)) { - try (PDDocument pdf = PDDocument.load(inputStream)) { + try (RandomAccessRead buffer = new RandomAccessReadBuffer(content)) { + try (PDDocument pdf = new PDFParser(buffer).parse()) { PDFTextStripper pdfTextStripper = new PDFTextStripper(); pdfTextStripper.setStartPage(startPage); pdfTextStripper.setEndPage(endPage); @@ -74,7 +76,7 @@ private PDF(String name, byte[] content, int startPage, int endPage) { public PDF(File pdfFile) throws IOException { this(pdfFile.getAbsolutePath(), readAllBytes(Paths.get(pdfFile.getAbsolutePath()))); } - + public PDF(URL url) throws IOException { this(url.toString(), readBytes(url)); } @@ -82,7 +84,7 @@ public PDF(URL url) throws IOException { public PDF(URI uri) throws IOException { this(uri.toURL()); } - + public PDF(byte[] content) { this("", content); } @@ -151,5 +153,4 @@ public static Matcher matchesText(String regex) { public static Matcher matchesText(Pattern regex) { return new MatchesText(regex); } - }