@@ -29,8 +29,10 @@ This file is part of the iText (R) project.
2929import com .itextpdf .kernel .pdf .PdfWriter ;
3030import com .itextpdf .pdfocr .OcrPdfCreator ;
3131import com .itextpdf .pdfocr .OcrPdfCreatorProperties ;
32+ import com .itextpdf .pdfocr .exceptions .PdfOcrInputException ;
3233import com .itextpdf .pdfocr .onnxtr .detection .IDetectionPredictor ;
3334import com .itextpdf .pdfocr .onnxtr .detection .OnnxDetectionPredictor ;
35+ import com .itextpdf .pdfocr .onnxtr .exceptions .PdfOcrOnnxTrExceptionMessageConstant ;
3436import com .itextpdf .pdfocr .onnxtr .orientation .IOrientationPredictor ;
3537import com .itextpdf .pdfocr .onnxtr .orientation .OnnxOrientationPredictor ;
3638import com .itextpdf .pdfocr .onnxtr .recognition .IRecognitionPredictor ;
@@ -83,16 +85,19 @@ public void rainbowInvertedCmykTest() throws IOException {
8385 String dest = TARGET_DIRECTORY + "rainbowInvertedCmykTest.pdf" ;
8486 String cmpTxt = TEST_DIRECTORY + "cmp_rainbowInvertedCmykTest.txt" ;
8587
86- if ( isFixedInJdk ()) {
88+ try {
8789 doOcrAndCreatePdf (src , dest , creatorProperties ("Text1" , DeviceCmyk .MAGENTA ));
8890 try (PdfDocument pdfDocument = new PdfDocument (new PdfReader (dest ))) {
8991 ExtractionStrategy extractionStrategy = OnnxTestUtils .extractTextFromLayer (pdfDocument , 1 , "Text1" );
9092 Assertions .assertEquals (DeviceCmyk .MAGENTA , extractionStrategy .getFillColor ());
9193 Assertions .assertEquals (getCmpText (cmpTxt ), extractionStrategy .getResultantText ());
9294 }
93- } else {
94- Exception e = Assertions .assertThrows (Exception .class , () -> doOcrAndCreatePdf (src , dest , null ));
95- Assertions .assertEquals ("Failed to read image." , e .getMessage ());
95+ } catch (PdfOcrInputException e ) {
96+ // CMYK bug https://bugs.openjdk.org/browse/JDK-8274735 in openJDK:
97+ // fixed for jdk8 from 351 onwards, for jdk11 from 16 onwards and for jdk17 starting from 4.
98+ // Amazon corretto jdk started support CMYK for JPEG from 11 version.
99+ // Temurin 8 does not support CMYK for JPEG either.
100+ Assertions .assertEquals (PdfOcrOnnxTrExceptionMessageConstant .FAILED_TO_READ_IMAGE , e .getMessage ());
96101 }
97102 }
98103
@@ -102,7 +107,7 @@ public void rainbowAdobeCmykTest() throws IOException {
102107 String dest = TARGET_DIRECTORY + "rainbowAdobeCmykTest.pdf" ;
103108 String cmpTxt = TEST_DIRECTORY + "cmp_rainbowAdobeCmykTest.txt" ;
104109
105- if ( isFixedInJdk ()) {
110+ try {
106111 doOcrAndCreatePdf (src , dest , creatorProperties ("Text1" , DeviceCmyk .MAGENTA ));
107112 try (PdfDocument pdfDocument = new PdfDocument (new PdfReader (dest ))) {
108113 ExtractionStrategy extractionStrategy = OnnxTestUtils .extractTextFromLayer (pdfDocument , 1 , "Text1" );
@@ -111,9 +116,12 @@ public void rainbowAdobeCmykTest() throws IOException {
111116 extractionStrategy .getResultantText ()) / getCmpText (cmpTxt ).length ();
112117 Assertions .assertTrue (relativeDistance < 0.05 );
113118 }
114- } else {
115- Exception e = Assertions .assertThrows (Exception .class , () -> doOcrAndCreatePdf (src , dest , null ));
116- Assertions .assertEquals ("Failed to read image." , e .getMessage ());
119+ } catch (PdfOcrInputException e ) {
120+ // CMYK bug https://bugs.openjdk.org/browse/JDK-8274735 in openJDK:
121+ // fixed for jdk8 from 351 onwards, for jdk11 from 16 onwards and for jdk17 starting from 4.
122+ // Amazon corretto jdk started support CMYK for JPEG from 11 version.
123+ // Temurin 8 does not support CMYK for JPEG either.
124+ Assertions .assertEquals (PdfOcrOnnxTrExceptionMessageConstant .FAILED_TO_READ_IMAGE , e .getMessage ());
117125 }
118126 }
119127
@@ -123,64 +131,22 @@ public void rainbowCmykNoProfileTest() throws IOException {
123131 String dest = TARGET_DIRECTORY + "rainbowCmykNoProfileTest.pdf" ;
124132 String cmpTxt = TEST_DIRECTORY + "cmp_rainbowCmykNoProfileTest.txt" ;
125133
126- if ( isFixedInJdk ()) {
134+ try {
127135 doOcrAndCreatePdf (src , dest , creatorProperties ("Text1" , DeviceCmyk .MAGENTA ));
128136 try (PdfDocument pdfDocument = new PdfDocument (new PdfReader (dest ))) {
129137 ExtractionStrategy extractionStrategy = OnnxTestUtils .extractTextFromLayer (pdfDocument , 1 , "Text1" );
130138 Assertions .assertEquals (DeviceCmyk .MAGENTA , extractionStrategy .getFillColor ());
131139 Assertions .assertEquals (getCmpText (cmpTxt ), extractionStrategy .getResultantText ());
132140 }
133- } else {
134- Exception e = Assertions .assertThrows (Exception .class , () -> doOcrAndCreatePdf (src , dest , null ));
135- Assertions .assertEquals ("Failed to read image." , e .getMessage ());
141+ } catch (PdfOcrInputException e ) {
142+ // CMYK bug https://bugs.openjdk.org/browse/JDK-8274735 in openJDK:
143+ // fixed for jdk8 from 351 onwards, for jdk11 from 16 onwards and for jdk17 starting from 4.
144+ // Amazon corretto jdk started support CMYK for JPEG from 11 version.
145+ // Temurin 8 does not support CMYK for JPEG either.
146+ Assertions .assertEquals (PdfOcrOnnxTrExceptionMessageConstant .FAILED_TO_READ_IMAGE , e .getMessage ());
136147 }
137148 }
138149
139- private static boolean isFixedInJdk () {
140- //Fixed CMYK bug https://bugs.openjdk.org/browse/JDK-8274735 for openJDK:
141- //jdk8 from 351 onwards, for jdk11 from 16 onwards and for jdk17 starting from 4.
142- //Amazon corretto jdk started support CMYK for JPEG from 11 version.
143- //Temurin 8 does not support CMYK for JPEG either.
144- String versionStr = System .getProperty ("java.version" );
145- String vendorStr = System .getProperty ("java.vendor" );
146- boolean isFixed = false ;
147- int majorVer = getMajorVer (versionStr );
148- String [] split = versionStr .split ("[._-]" );
149- int minorVer = Integer .parseInt (split [split .length - 1 ]);
150-
151- switch (majorVer ) {
152- case 8 :
153- if ("Amazon.com Inc." .equals (vendorStr ) || "Temurin" .equals (vendorStr )) {
154- return false ;
155- }
156-
157- isFixed = minorVer >= 351 ;
158- break ;
159- case 11 :
160- isFixed = minorVer >= 16 ;
161- break ;
162- case 17 :
163- isFixed = minorVer >= 4 ;
164- break ;
165- default :
166- isFixed = true ;
167- }
168-
169- return isFixed ;
170- }
171-
172- private static int getMajorVer (String versionStr ) {
173- int majorVer = 0 ;
174- String [] split = versionStr .split ("\\ ." );
175- if (versionStr .startsWith ("1." )) {
176- //jdk versions 1 - 8 have 1. as prefix
177- majorVer = Integer .parseInt (split [1 ]);
178- } else {
179- majorVer = Integer .parseInt (split [0 ]);
180- }
181- return majorVer ;
182- }
183-
184150 private OcrPdfCreatorProperties creatorProperties (String layerName , Color color ) {
185151 OcrPdfCreatorProperties ocrPdfCreatorProperties = new OcrPdfCreatorProperties ();
186152 ocrPdfCreatorProperties .setTextLayerName (layerName );
0 commit comments