11package com .mindee .input ;
22
33import com .mindee .image .ImageCompressor ;
4- import com .mindee .pdf .PDFBoxApi ;
4+ import com .mindee .pdf .PDFCompression ;
55import com .mindee .pdf .PDFCompressor ;
6- import com .mindee .pdf .PDFOperation ;
6+ import com .mindee .pdf .PDFInputOperation ;
7+ import com .mindee .pdf .PDFInputOperator ;
78import java .io .File ;
89import java .io .IOException ;
910import java .io .InputStream ;
1718/**
1819 * A source document for Mindee API operations.
1920 */
20- public final class LocalInputSource {
21+ public class LocalInputSource {
2122
2223 @ Getter
2324 private byte [] file ;
2425 @ Getter
2526 private final String filename ;
2627 @ Setter
27- private PDFOperation pdfOperation ;
28+ private PDFInputOperation pdfInputOperator ;
29+ @ Setter
30+ private PDFCompression pdfCompressor ;
31+ // Store here to avoid recalculating every time.
32+ private Boolean isPDF ;
2833
2934 public LocalInputSource (InputStream file , String filename ) throws IOException {
3035 this .file = IOUtils .toByteArray (file );
@@ -57,11 +62,18 @@ public LocalInputSource(String fileAsBase64, String filename) {
5762 this .filename = filename ;
5863 }
5964
60- public PDFOperation getPdfOperation () {
61- if (this .pdfOperation == null ) {
62- this .pdfOperation = new PDFBoxApi ();
65+ private PDFInputOperation getPDFInputOperator () {
66+ if (this .pdfInputOperator == null ) {
67+ this .pdfInputOperator = new PDFInputOperator ();
68+ }
69+ return this .pdfInputOperator ;
70+ }
71+
72+ private PDFCompression getPDFCompressor () {
73+ if (this .pdfCompressor == null ) {
74+ this .pdfCompressor = new PDFCompressor ();
6375 }
64- return this .pdfOperation ;
76+ return this .pdfCompressor ;
6577 }
6678
6779 /**
@@ -71,10 +83,10 @@ public PDFOperation getPdfOperation() {
7183 * @throws IOException If an I/O error occurs during the PDF operation.
7284 */
7385 public int getPageCount () throws IOException {
74- if (!this .isPdf ()) {
86+ if (!this .isPDF ()) {
7587 return 1 ;
7688 }
77- return getPdfOperation ().getNumberOfPages (this .file );
89+ return getPDFInputOperator ().getPageCount (this .file );
7890 }
7991
8092 /**
@@ -84,51 +96,58 @@ public int getPageCount() throws IOException {
8496 * @throws IOException If an I/O error occurs during the PDF operation.
8597 */
8698 public void applyPageOptions (PageOptions pageOptions ) throws IOException {
87- if (pageOptions != null && this .isPdf ()) {
88- this .file = getPdfOperation ().split (this .file , pageOptions ).getFile ();
99+ if (pageOptions != null && this .isPDF ()) {
100+ this .file = getPDFInputOperator ().split (this .file , pageOptions ).getFile ();
89101 }
90102 }
91103
92- public boolean isPdf () {
93- return InputSourceUtils .isPdf (this .file );
94- }
95-
96- public boolean hasSourceText () {
97- return InputSourceUtils .hasSourceText (this .file );
104+ /**
105+ * Returns true if the file is a PDF.
106+ */
107+ public boolean isPDF () {
108+ if (this .isPDF == null ) {
109+ this .isPDF = getPDFInputOperator ().isPDF (this .file );
110+ }
111+ return this .isPDF ;
98112 }
99113
100114 public void compress (
101- Integer quality ,
115+ int quality ,
102116 Integer maxWidth ,
103117 Integer maxHeight ,
104118 Boolean forceSourceText ,
105119 Boolean disableSourceText
106120 ) throws IOException {
107- if (isPdf ()) {
108- this .file = PDFCompressor .compressPdf (this .file , quality , forceSourceText , disableSourceText );
121+ if (isPDF ()) {
122+ this .file = getPDFCompressor ()
123+ .compressPDF (this .file , quality , forceSourceText , disableSourceText );
109124 } else {
110125 this .file = ImageCompressor .compressImage (this .file , quality , maxWidth , maxHeight );
111126 }
112127 }
113128
114129 public void compress (
115- Integer quality ,
130+ int quality ,
116131 Integer maxWidth ,
117132 Integer maxHeight ,
118133 Boolean forceSourceText
119134 ) throws IOException {
120135 this .compress (quality , maxWidth , maxHeight , forceSourceText , true );
121136 }
122137
123- public void compress (Integer quality , Integer maxWidth , Integer maxHeight ) throws IOException {
124- this .compress (quality , maxWidth , maxHeight , false , true );
138+ public void compress (
139+ int quality ,
140+ boolean forceSourceText ,
141+ boolean disableSourceText
142+ ) throws IOException {
143+ this .compress (quality , null , null , forceSourceText , disableSourceText );
125144 }
126145
127- public void compress (Integer quality , Integer maxWidth ) throws IOException {
128- this .compress (quality , maxWidth , null , false , true );
146+ public void compress (int quality , Integer maxWidth , Integer maxHeight ) throws IOException {
147+ this .compress (quality , maxWidth , maxHeight , false , true );
129148 }
130149
131- public void compress (Integer quality ) throws IOException {
150+ public void compress (int quality ) throws IOException {
132151 this .compress (quality , null , null , false , true );
133152 }
134153
0 commit comments