Skip to content

Commit 2934e70

Browse files
author
dmitry.radchuk
committed
Added input related exceptions in pdfOcr
DEVSIX-5742
1 parent 6736eea commit 2934e70

32 files changed

+639
-301
lines changed

pdfocr-api/src/main/java/com/itextpdf/pdfocr/OcrPdfCreator.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ This file is part of the iText (R) project.
5757
import com.itextpdf.layout.font.FontProvider;
5858
import com.itextpdf.layout.properties.TextAlignment;
5959
import com.itextpdf.pdfa.PdfADocument;
60-
import com.itextpdf.pdfocr.exceptions.OcrException;
60+
import com.itextpdf.pdfocr.exceptions.PdfOcrException;
61+
import com.itextpdf.pdfocr.exceptions.PdfOcrExceptionMessageConstant;
6162
import com.itextpdf.pdfocr.logs.PdfOcrLogMessageConstant;
6263
import com.itextpdf.pdfocr.statistics.PdfOcrOutputType;
6364
import com.itextpdf.pdfocr.statistics.PdfOcrOutputTypeStatisticsEvent;
@@ -165,14 +166,14 @@ public final void setOcrPdfCreatorProperties(
165166
* @return result PDF/A-3u {@link com.itextpdf.kernel.pdf.PdfDocument}
166167
* object
167168
*
168-
* @throws OcrException if it was not possible to read provided or
169+
* @throws PdfOcrException if it was not possible to read provided or
169170
* default font
170171
*/
171172
public final PdfDocument createPdfA(final List<File> inputImages,
172173
final PdfWriter pdfWriter,
173174
final DocumentProperties documentProperties,
174175
final PdfOutputIntent pdfOutputIntent)
175-
throws OcrException {
176+
throws PdfOcrException {
176177
LOGGER.info(MessageFormatUtil.format(
177178
PdfOcrLogMessageConstant.START_OCR_FOR_IMAGES,
178179
inputImages.size()));
@@ -220,13 +221,13 @@ public final PdfDocument createPdfA(final List<File> inputImages,
220221
* @return result PDF/A-3u {@link com.itextpdf.kernel.pdf.PdfDocument}
221222
* object
222223
*
223-
* @throws OcrException if it was not possible to read provided or
224+
* @throws PdfOcrException if it was not possible to read provided or
224225
* default font
225226
*/
226227
public final PdfDocument createPdfA(final List<File> inputImages,
227228
final PdfWriter pdfWriter,
228229
final PdfOutputIntent pdfOutputIntent)
229-
throws OcrException {
230+
throws PdfOcrException {
230231
return createPdfA(inputImages, pdfWriter, new DocumentProperties(), pdfOutputIntent);
231232
}
232233

@@ -247,12 +248,12 @@ public final PdfDocument createPdfA(final List<File> inputImages,
247248
*
248249
* @return result {@link com.itextpdf.kernel.pdf.PdfDocument} object
249250
*
250-
* @throws OcrException if provided font is incorrect
251+
* @throws PdfOcrException if provided font is incorrect
251252
*/
252253
public final PdfDocument createPdf(final List<File> inputImages,
253254
final PdfWriter pdfWriter,
254255
final DocumentProperties documentProperties)
255-
throws OcrException {
256+
throws PdfOcrException {
256257
return createPdfA(inputImages, pdfWriter, documentProperties, null);
257258
}
258259

@@ -272,11 +273,11 @@ public final PdfDocument createPdf(final List<File> inputImages,
272273
*
273274
* @return result {@link com.itextpdf.kernel.pdf.PdfDocument} object
274275
*
275-
* @throws OcrException if provided font is incorrect
276+
* @throws PdfOcrException if provided font is incorrect
276277
*/
277278
public final PdfDocument createPdf(final List<File> inputImages,
278279
final PdfWriter pdfWriter)
279-
throws OcrException {
280+
throws PdfOcrException {
280281
return createPdfA(inputImages, pdfWriter, new DocumentProperties(), null);
281282
}
282283

@@ -288,12 +289,12 @@ public final PdfDocument createPdf(final List<File> inputImages,
288289
* @param outPdfFile the {@link java.io.File} object to write final PDF document to
289290
*
290291
* @throws IOException signals that an I/O exception of some sort has occurred.
291-
* @throws OcrException if it was not possible to read provided or
292+
* @throws PdfOcrException if it was not possible to read provided or
292293
* default font
293294
*/
294295
public void createPdfFile(final List<File> inputImages,
295296
final File outPdfFile)
296-
throws OcrException, IOException {
297+
throws PdfOcrException, IOException {
297298
createPdfAFile(inputImages, outPdfFile, null);
298299
}
299300

@@ -308,13 +309,13 @@ public void createPdfFile(final List<File> inputImages,
308309
* for PDF/A-3u document
309310
*
310311
* @throws IOException signals that an I/O exception of some sort has occurred
311-
* @throws OcrException if it was not possible to read provided or
312+
* @throws PdfOcrException if it was not possible to read provided or
312313
* default font
313314
*/
314315
public void createPdfAFile(final List<File> inputImages,
315316
final File outPdfFile,
316317
final PdfOutputIntent pdfOutputIntent)
317-
throws OcrException, IOException {
318+
throws PdfOcrException, IOException {
318319
DocumentProperties documentProperties = new DocumentProperties();
319320
if (ocrPdfCreatorProperties.getMetaInfo() != null) {
320321
documentProperties.setEventCountingMetaInfo(ocrPdfCreatorProperties.getMetaInfo());
@@ -356,13 +357,13 @@ public final void setOcrEngine(final IOcrEngine reader) {
356357
* @param imageData input image if it is a single page or its one page if
357358
* this is a multi-page image
358359
* @param createPdfA3u true if PDF/A3u document is being created
359-
* @throws OcrException if PDF/A3u document is being created and provided
360+
* @throws PdfOcrException if PDF/A3u document is being created and provided
360361
* font contains notdef glyphs
361362
*/
362363
private void addToCanvas(final PdfDocument pdfDocument,
363364
final Rectangle imageSize,
364365
final List<TextInfo> pageText, final ImageData imageData,
365-
final boolean createPdfA3u) throws OcrException {
366+
final boolean createPdfA3u) throws PdfOcrException {
366367
final Rectangle rectangleSize =
367368
ocrPdfCreatorProperties.getPageSize() == null
368369
? imageSize : ocrPdfCreatorProperties.getPageSize();
@@ -393,11 +394,11 @@ private void addToCanvas(final PdfDocument pdfDocument,
393394
try {
394395
addTextToCanvas(imageSize, pageText, canvas, multiplier,
395396
pdfPage.getMediaBox());
396-
} catch (OcrException e) {
397+
} catch (PdfOcrException e) {
397398
LOGGER.error(MessageFormatUtil.format(
398-
OcrException.CANNOT_CREATE_PDF_DOCUMENT,
399+
PdfOcrExceptionMessageConstant.CANNOT_CREATE_PDF_DOCUMENT,
399400
e.getMessage()));
400-
throw new OcrException(OcrException.CANNOT_CREATE_PDF_DOCUMENT)
401+
throw new PdfOcrException(PdfOcrExceptionMessageConstant.CANNOT_CREATE_PDF_DOCUMENT)
401402
.setMessageParams(e.getMessage());
402403
}
403404
if (layers[1] != null) {
@@ -427,9 +428,9 @@ private PdfDocument createPdfDocument(final PdfWriter pdfWriter,
427428
&& !ocrPdfCreatorProperties.getPdfLang().equals("");
428429
if (createPdfA3u && !hasPdfLangProperty) {
429430
LOGGER.error(MessageFormatUtil.format(
430-
OcrException.CANNOT_CREATE_PDF_DOCUMENT,
431+
PdfOcrExceptionMessageConstant.CANNOT_CREATE_PDF_DOCUMENT,
431432
PdfOcrLogMessageConstant.PDF_LANGUAGE_PROPERTY_IS_NOT_SET));
432-
throw new OcrException(OcrException.CANNOT_CREATE_PDF_DOCUMENT)
433+
throw new PdfOcrException(PdfOcrExceptionMessageConstant.CANNOT_CREATE_PDF_DOCUMENT)
433434
.setMessageParams(PdfOcrLogMessageConstant.PDF_LANGUAGE_PROPERTY_IS_NOT_SET);
434435
}
435436

@@ -472,13 +473,13 @@ private PdfDocument createPdfDocument(final PdfWriter pdfWriter,
472473
* map pageNumber -> text for the page
473474
* @param pdfDocument result {@link com.itextpdf.kernel.pdf.PdfDocument}
474475
* @param createPdfA3u true if PDF/A3u document is being created
475-
* @throws OcrException if input image cannot be read or provided font
476+
* @throws PdfOcrException if input image cannot be read or provided font
476477
* contains NOTDEF glyphs
477478
*/
478479
private void addDataToPdfDocument(
479480
final Map<File, Map<Integer, List<TextInfo>>> imagesTextData,
480481
final PdfDocument pdfDocument,
481-
final boolean createPdfA3u) throws OcrException {
482+
final boolean createPdfA3u) throws PdfOcrException {
482483
for (Map.Entry<File, Map<Integer, List<TextInfo>>> entry
483484
: imagesTextData.entrySet()) {
484485
File inputImage = entry.getKey();
@@ -545,7 +546,7 @@ private void addImageToCanvas(final ImageData imageData,
545546
* @param pdfCanvas canvas to place the text
546547
* @param multiplier coefficient to adjust text placing on canvas
547548
* @param pageMediaBox page parameters
548-
* @throws OcrException if PDF/A3u document is being created and provided
549+
* @throws PdfOcrException if PDF/A3u document is being created and provided
549550
* font contains notdef glyphs
550551
*/
551552
private void addTextToCanvas(
@@ -554,7 +555,7 @@ private void addTextToCanvas(
554555
final PdfCanvas pdfCanvas,
555556
final float multiplier,
556557
final Rectangle pageMediaBox)
557-
throws OcrException {
558+
throws PdfOcrException {
558559
if (pageText != null && pageText.size() > 0) {
559560
final Point imageCoordinates =
560561
PdfCreatorUtil.calculateImageCoordinates(
@@ -750,7 +751,7 @@ public PdfCanvas showText(GlyphLine text) {
750751
if (this.createPdfA3u) {
751752
// exception is thrown only if PDF/A document is
752753
// being created
753-
throw new OcrException(message);
754+
throw new PdfOcrException(message);
754755
}
755756
// setting actual text to NotDef glyph
756757
glyphLine.setActualTextToGlyph(i,

pdfocr-api/src/main/java/com/itextpdf/pdfocr/PdfCreatorUtil.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ This file is part of the iText (R) project.
3838
import com.itextpdf.layout.layout.LayoutResult;
3939
import com.itextpdf.layout.renderer.IRenderer;
4040
import com.itextpdf.layout.renderer.ParagraphRenderer;
41-
import com.itextpdf.pdfocr.exceptions.OcrException;
41+
import com.itextpdf.pdfocr.exceptions.PdfOcrException;
42+
import com.itextpdf.pdfocr.exceptions.PdfOcrExceptionMessageConstant;
43+
import com.itextpdf.pdfocr.exceptions.PdfOcrInputException;
4244
import com.itextpdf.pdfocr.logs.PdfOcrLogMessageConstant;
4345

4446
import java.io.File;
@@ -80,12 +82,12 @@ class PdfCreatorUtil {
8082
* @param bboxHeightPt height of bbox calculated by OCR Reader
8183
* @param bboxWidthPt width of bbox calculated by OCR Reader
8284
* @return font size
83-
* @throws OcrException if set font provider is invalid and/or fonts that
85+
* @throws PdfOcrException if set font provider is invalid and/or fonts that
8486
* it contains are invalid
8587
*/
8688
static float calculateFontSize(final Document document, final String line,
8789
final String fontFamily, final float bboxHeightPt,
88-
final float bboxWidthPt) throws OcrException {
90+
final float bboxWidthPt) throws PdfOcrException {
8991
Rectangle bbox = new Rectangle(bboxWidthPt * 1.5f,
9092
bboxHeightPt * 1.5f);
9193
// setting minimum and maximum (approx.) values for font size
@@ -113,8 +115,8 @@ static float calculateFontSize(final Document document, final String line,
113115
} catch (IllegalStateException e) {
114116
LOGGER.error(PdfOcrLogMessageConstant
115117
.PROVIDED_FONT_PROVIDER_IS_INVALID);
116-
throw new OcrException(
117-
OcrException.CANNOT_RESOLVE_PROVIDED_FONTS, e);
118+
throw new PdfOcrInputException(
119+
PdfOcrExceptionMessageConstant.CANNOT_RESOLVE_PROVIDED_FONTS, e);
118120
}
119121
return fontSize;
120122
}
@@ -171,11 +173,11 @@ static com.itextpdf.kernel.geom.Point calculateImageCoordinates(
171173
* @param imageRotationHandler image rotation handler {@link IImageRotationHandler}
172174
* @return list of {@link com.itextpdf.io.image.ImageData} objects
173175
* (more than one element in the list if it is a multipage tiff)
174-
* @throws OcrException if error occurred during reading a file
176+
* @throws PdfOcrException if error occurred during reading a file
175177
* @throws IOException if error occurred during reading a file
176178
*/
177179
static List<ImageData> getImageData(final File inputImage, IImageRotationHandler imageRotationHandler)
178-
throws OcrException {
180+
throws PdfOcrException {
179181
List<ImageData> images = new ArrayList<ImageData>();
180182

181183
try (InputStream imageStream = new FileInputStream(inputImage)) {
@@ -205,8 +207,8 @@ static List<ImageData> getImageData(final File inputImage, IImageRotationHandler
205207
LOGGER.error(MessageFormatUtil.format(
206208
PdfOcrLogMessageConstant.CANNOT_READ_INPUT_IMAGE,
207209
e.getMessage()));
208-
throw new OcrException(
209-
OcrException.CANNOT_READ_INPUT_IMAGE, e);
210+
throw new PdfOcrInputException(
211+
PdfOcrExceptionMessageConstant.CANNOT_READ_INPUT_IMAGE, e);
210212
}
211213
return images;
212214
}

pdfocr-api/src/main/java/com/itextpdf/pdfocr/exceptions/OcrException.java renamed to pdfocr-api/src/main/java/com/itextpdf/pdfocr/exceptions/PdfOcrException.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,50 @@ This file is part of the iText (R) project.
3131
/**
3232
* Exception class for custom exceptions.
3333
*/
34-
public class OcrException extends ITextException {
35-
36-
public static final String CANNOT_READ_INPUT_IMAGE =
37-
"Cannot read input image";
38-
public static final String CANNOT_RESOLVE_PROVIDED_FONTS = "Cannot resolve "
39-
+ "any of provided fonts. Please check provided FontProvider.";
40-
public static final String CANNOT_CREATE_PDF_DOCUMENT = "Cannot create "
41-
+ "PDF document: {0}";
42-
public static final String STATISTICS_EVENT_TYPE_CANT_BE_NULL = "Statistics event type can't be null";
43-
public static final String STATISTICS_EVENT_TYPE_IS_NOT_DETECTED = "Statistics event type is not detected.";
34+
public class PdfOcrException extends ITextException {
35+
4436
private List<String> messageParams;
4537

4638
/**
47-
* Creates a new OcrException.
39+
* Creates a new {@link PdfOcrException}.
4840
*
4941
* @param msg the detail message.
5042
* @param e the cause
5143
* (which is saved for later retrieval
5244
* by {@link #getCause()} method).
5345
*/
54-
public OcrException(String msg, Throwable e) {
46+
public PdfOcrException(String msg, Throwable e) {
5547
super(msg, e);
5648
}
5749

5850
/**
59-
* Creates a new OcrException.
51+
* Creates a new {@link PdfOcrException}.
6052
*
6153
* @param msg the detail message.
6254
*/
63-
public OcrException(String msg) {
55+
public PdfOcrException(String msg) {
6456
super(msg);
6557
}
6658

59+
/**
60+
* Creates a new {@link PdfOcrException}.
61+
*
62+
* @param e the cause
63+
* which is saved for later retrieval
64+
* by {@link #getCause()} method).
65+
*/
66+
public PdfOcrException(Throwable e) {
67+
super(e);
68+
}
69+
6770
/**
6871
* {@inheritDoc}
6972
*/
7073
@Override
7174
public String getMessage() {
7275
return this.messageParams != null && this.messageParams.size() != 0
7376
? MessageFormatUtil
74-
.format(super.getMessage(), this.getMessageParams())
77+
.format(super.getMessage(), this.getMessageParams())
7578
: super.getMessage();
7679
}
7780

@@ -94,7 +97,7 @@ protected Object[] getMessageParams() {
9497
* @param messageParams additional params.
9598
* @return object itself.
9699
*/
97-
public OcrException setMessageParams(String... messageParams) {
100+
public PdfOcrException setMessageParams(String... messageParams) {
98101
this.messageParams = Arrays.<String>asList(messageParams);
99102
return this;
100103
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.pdfocr.exceptions;
24+
25+
public class PdfOcrExceptionMessageConstant {
26+
27+
public static final String CANNOT_READ_INPUT_IMAGE = "Cannot read input image";
28+
public static final String CANNOT_RESOLVE_PROVIDED_FONTS =
29+
"Cannot resolve any of provided fonts. Please check provided FontProvider.";
30+
public static final String CANNOT_CREATE_PDF_DOCUMENT = "Cannot create PDF document: {0}";
31+
public static final String STATISTICS_EVENT_TYPE_CANT_BE_NULL = "Statistics event type can't be null";
32+
public static final String STATISTICS_EVENT_TYPE_IS_NOT_DETECTED = "Statistics event type is not detected.";
33+
34+
private PdfOcrExceptionMessageConstant() {
35+
//Private constructor will prevent the instantiation of this class directly
36+
}
37+
}

0 commit comments

Comments
 (0)