@@ -218,7 +218,7 @@ public void checkColor(Color color, PdfDictionary currentColorSpaces, Boolean fi
218218 if (pattern instanceof PdfPattern .Shading ) {
219219 PdfDictionary shadingDictionary = ((PdfPattern .Shading ) pattern ).getShading ();
220220 PdfObject colorSpace = shadingDictionary .get (PdfName .ColorSpace );
221- checkColorSpace (PdfColorSpace .makeColorSpace (colorSpace ), currentColorSpaces , true , true );
221+ checkColorSpace (PdfColorSpace .makeColorSpace (colorSpace ), contentStream , currentColorSpaces , true , true );
222222 final PdfDictionary extGStateDict = ((PdfDictionary ) pattern .getPdfObject ()).getAsDictionary (PdfName .ExtGState );
223223 CanvasGraphicsState gState = new UpdateCanvasGraphicsState (extGStateDict );
224224 checkExtGState (gState , contentStream );
@@ -230,8 +230,12 @@ public void checkColor(Color color, PdfDictionary currentColorSpaces, Boolean fi
230230 super .checkColor (color , currentColorSpaces , fill , contentStream );
231231 }
232232
233+ /**
234+ * {@inheritDoc}
235+ */
233236 @ Override
234- public void checkColorSpace (PdfColorSpace colorSpace , PdfDictionary currentColorSpaces , boolean checkAlternate , Boolean fill ) {
237+ public void checkColorSpace (PdfColorSpace colorSpace , PdfObject pdfObject , PdfDictionary currentColorSpaces ,
238+ boolean checkAlternate , Boolean fill ) {
235239 if (fill != null ) {
236240 if ((boolean ) fill ) {
237241 currentFillCsIsIccBasedCMYK = false ;
@@ -245,7 +249,7 @@ public void checkColorSpace(PdfColorSpace colorSpace, PdfDictionary currentColor
245249 PdfSpecialCs .Separation separation = (PdfSpecialCs .Separation ) colorSpace ;
246250 checkSeparationCS ((PdfArray ) separation .getPdfObject ());
247251 if (checkAlternate ) {
248- checkColorSpace (separation .getBaseCs (), currentColorSpaces , false , fill );
252+ checkColorSpace (separation .getBaseCs (), pdfObject , currentColorSpaces , false , fill );
249253 }
250254
251255 } else if (colorSpace instanceof PdfSpecialCs .DeviceN ) {
@@ -266,30 +270,30 @@ public void checkColorSpace(PdfColorSpace colorSpace, PdfDictionary currentColor
266270 }
267271
268272 if (checkAlternate ) {
269- checkColorSpace (deviceN .getBaseCs (), currentColorSpaces , false , fill );
273+ checkColorSpace (deviceN .getBaseCs (), pdfObject , currentColorSpaces , false , fill );
270274 }
271275
272276 } else if (colorSpace instanceof PdfSpecialCs .Indexed ) {
273277 if (checkAlternate ) {
274- checkColorSpace (((PdfSpecialCs .Indexed ) colorSpace ).getBaseCs (), currentColorSpaces , true , fill );
278+ checkColorSpace (((PdfSpecialCs .Indexed ) colorSpace ).getBaseCs (), pdfObject , currentColorSpaces , true , fill );
275279 }
276280 } else if (colorSpace instanceof PdfSpecialCs .UncoloredTilingPattern ) {
277281 if (checkAlternate ) {
278- checkColorSpace (((PdfSpecialCs .UncoloredTilingPattern ) colorSpace ).getUnderlyingColorSpace (), currentColorSpaces , true , fill );
282+ checkColorSpace (((PdfSpecialCs .UncoloredTilingPattern ) colorSpace ).getUnderlyingColorSpace (), pdfObject ,
283+ currentColorSpaces , true , fill );
279284 }
280285 } else {
281-
282286 if (colorSpace instanceof PdfDeviceCs .Rgb ) {
283- if (!checkDefaultCS (currentColorSpaces , fill , PdfName .DefaultRGB , 3 )) {
284- rgbIsUsed = true ;
287+ if (!checkDefaultCS (pdfObject , currentColorSpaces , fill , PdfName .DefaultRGB , 3 )) {
288+ rgbUsedObjects . add ( pdfObject ) ;
285289 }
286290 } else if (colorSpace instanceof PdfDeviceCs .Cmyk ) {
287- if (!checkDefaultCS (currentColorSpaces , fill , PdfName .DefaultCMYK , 4 )) {
288- cmykIsUsed = true ;
291+ if (!checkDefaultCS (pdfObject , currentColorSpaces , fill , PdfName .DefaultCMYK , 4 )) {
292+ cmykUsedObjects . add ( pdfObject ) ;
289293 }
290294 } else if (colorSpace instanceof PdfDeviceCs .Gray ) {
291- if (!checkDefaultCS (currentColorSpaces , fill , PdfName .DefaultGray , 1 )) {
292- grayIsUsed = true ;
295+ if (!checkDefaultCS (pdfObject , currentColorSpaces , fill , PdfName .DefaultGray , 1 )) {
296+ grayUsedObjects . add ( pdfObject ) ;
293297 }
294298 }
295299 }
@@ -544,7 +548,7 @@ protected void checkAppearanceStream(PdfStream appearanceStream) {
544548 if (isContainsTransparencyGroup (appearanceStream )) {
545549 this .transparencyObjects .add (appearanceStream );
546550 }
547- checkResources (appearanceStream .getAsDictionary (PdfName .Resources ));
551+ checkResources (appearanceStream .getAsDictionary (PdfName .Resources ), appearanceStream );
548552 }
549553
550554 @ Override
@@ -557,14 +561,14 @@ protected void checkForm(PdfDictionary form) {
557561 if (form .containsKey (PdfName .XFA )) {
558562 throw new PdfAConformanceException (PdfaExceptionMessageConstant .THE_INTERACTIVE_FORM_DICTIONARY_SHALL_NOT_CONTAIN_THE_XFA_KEY );
559563 }
560- checkResources (form .getAsDictionary (PdfName .DR ));
564+ checkResources (form .getAsDictionary (PdfName .DR ), form );
561565
562566 PdfArray fields = form .getAsArray (PdfName .Fields );
563567 if (fields != null ) {
564568 fields = getFormFields (fields );
565569 for (PdfObject field : fields ) {
566570 PdfDictionary fieldDic = (PdfDictionary ) field ;
567- checkResources (fieldDic .getAsDictionary (PdfName .DR ));
571+ checkResources (fieldDic .getAsDictionary (PdfName .DR ), fieldDic );
568572 }
569573 }
570574 }
@@ -746,7 +750,7 @@ protected void checkPageObject(PdfDictionary pageDict, PdfDictionary pageResourc
746750 PdfObject cs = pageDict .getAsDictionary (PdfName .Group ).get (PdfName .CS );
747751 if (cs != null ) {
748752 PdfDictionary currentColorSpaces = pageResources .getAsDictionary (PdfName .ColorSpace );
749- checkColorSpace (PdfColorSpace .makeColorSpace (cs ), currentColorSpaces , true , null );
753+ checkColorSpace (PdfColorSpace .makeColorSpace (cs ), pageDict , currentColorSpaces , true , null );
750754 }
751755 }
752756 }
@@ -803,18 +807,22 @@ protected Set<PdfName> getAllowedNamedActions() {
803807 return allowedNamedActions ;
804808 }
805809
810+ /**
811+ * {@inheritDoc}
812+ */
806813 @ Override
807- protected void checkColorsUsages () {
808- if ((rgbIsUsed || cmykIsUsed || grayIsUsed ) && pdfAOutputIntentColorSpace == null ) {
814+ protected void checkPageColorsUsages (PdfDictionary pageDict , PdfDictionary pageResources ) {
815+ if ((rgbIsUsed || cmykIsUsed || grayIsUsed || !rgbUsedObjects .isEmpty () || !cmykUsedObjects .isEmpty () ||
816+ !grayUsedObjects .isEmpty ()) && pdfAOutputIntentColorSpace == null ) {
809817 throw new PdfAConformanceException (PdfaExceptionMessageConstant .IF_DEVICE_RGB_CMYK_GRAY_USED_IN_FILE_THAT_FILE_SHALL_CONTAIN_PDFA_OUTPUTINTENT_OR_DEFAULT_RGB_CMYK_GRAY_IN_USAGE_CONTEXT );
810818 }
811819
812- if (rgbIsUsed ) {
820+ if (rgbIsUsed || ! rgbUsedObjects . isEmpty () ) {
813821 if (!ICC_COLOR_SPACE_RGB .equals (pdfAOutputIntentColorSpace )) {
814822 throw new PdfAConformanceException (PdfaExceptionMessageConstant .DEVICERGB_MAY_BE_USED_ONLY_IF_THE_FILE_HAS_A_RGB_PDFA_OUTPUT_INTENT_OR_DEFAULTRGB_IN_USAGE_CONTEXT );
815823 }
816824 }
817- if (cmykIsUsed ) {
825+ if (cmykIsUsed || ! cmykUsedObjects . isEmpty () ) {
818826 if (!ICC_COLOR_SPACE_CMYK .equals (pdfAOutputIntentColorSpace )) {
819827 throw new PdfAConformanceException (PdfaExceptionMessageConstant .DEVICECMYK_MAY_BE_USED_ONLY_IF_THE_FILE_HAS_A_CMYK_PDFA_OUTPUT_INTENT_OR_DEFAULTCMYK_IN_USAGE_CONTEXT );
820828 }
@@ -826,14 +834,14 @@ protected void checkImage(PdfStream image, PdfDictionary currentColorSpaces) {
826834 PdfColorSpace colorSpace = null ;
827835 if (isAlreadyChecked (image )) {
828836 colorSpace = checkedObjectsColorspace .get (image );
829- checkColorSpace (colorSpace , currentColorSpaces , true , null );
837+ checkColorSpace (colorSpace , image , currentColorSpaces , true , null );
830838 return ;
831839 }
832840
833841 PdfObject colorSpaceObj = image .get (PdfName .ColorSpace );
834842 if (colorSpaceObj != null ) {
835843 colorSpace = PdfColorSpace .makeColorSpace (colorSpaceObj );
836- checkColorSpace (colorSpace , currentColorSpaces , true , null );
844+ checkColorSpace (colorSpace , image , currentColorSpaces , true , null );
837845 checkedObjectsColorspace .put (image , colorSpace );
838846 }
839847
@@ -900,17 +908,17 @@ protected void checkImage(PdfStream image, PdfDictionary currentColorSpaces) {
900908 switch (colorSpecBox .getEnumCs ()) {
901909 case 1 :
902910 PdfDeviceCs .Gray deviceGrayCs = new PdfDeviceCs .Gray ();
903- checkColorSpace (deviceGrayCs , currentColorSpaces , true , null );
911+ checkColorSpace (deviceGrayCs , image , currentColorSpaces , true , null );
904912 checkedObjectsColorspace .put (image , deviceGrayCs );
905913 break ;
906914 case 3 :
907915 PdfDeviceCs .Rgb deviceRgbCs = new PdfDeviceCs .Rgb ();
908- checkColorSpace (deviceRgbCs , currentColorSpaces , true , null );
916+ checkColorSpace (deviceRgbCs , image , currentColorSpaces , true , null );
909917 checkedObjectsColorspace .put (image , deviceRgbCs );
910918 break ;
911919 case 12 :
912920 PdfDeviceCs .Cmyk deviceCmykCs = new PdfDeviceCs .Cmyk ();
913- checkColorSpace (deviceCmykCs , currentColorSpaces , true , null );
921+ checkColorSpace (deviceCmykCs , image , currentColorSpaces , true , null );
914922 checkedObjectsColorspace .put (image , deviceCmykCs );
915923 break ;
916924 }
@@ -972,7 +980,7 @@ protected void checkFormXObject(PdfStream form, PdfStream contentStream) {
972980
973981 checkTransparencyGroup (form , contentStream );
974982
975- checkResources (form .getAsDictionary (PdfName .Resources ));
983+ checkResources (form .getAsDictionary (PdfName .Resources ), contentStream != null ? contentStream : form );
976984 checkContentStream (form );
977985 }
978986
@@ -987,7 +995,8 @@ protected void checkTransparencyGroup(PdfStream form, PdfStream contentStream) {
987995 PdfDictionary resources = form .getAsDictionary (PdfName .Resources );
988996 if (cs != null && resources != null ) {
989997 PdfDictionary currentColorSpaces = resources .getAsDictionary (PdfName .ColorSpace );
990- checkColorSpace (PdfColorSpace .makeColorSpace (cs ), currentColorSpaces , true , null );
998+ checkColorSpace (PdfColorSpace .makeColorSpace (cs ), contentStream != null ? contentStream : form ,
999+ currentColorSpaces , true , null );
9911000 }
9921001 }
9931002 }
@@ -1184,7 +1193,8 @@ private void fillOrderRecursively(PdfArray orderArray, Set<PdfObject> order) {
11841193 }
11851194 }
11861195
1187- private boolean checkDefaultCS (PdfDictionary currentColorSpaces , Boolean fill , PdfName defaultCsName , int numOfComponents ) {
1196+ private boolean checkDefaultCS (PdfObject pdfObject , PdfDictionary currentColorSpaces , Boolean fill , PdfName defaultCsName ,
1197+ int numOfComponents ) {
11881198 if (currentColorSpaces == null )
11891199 return false ;
11901200 if (!currentColorSpaces .containsKey (defaultCsName ))
@@ -1198,7 +1208,7 @@ private boolean checkDefaultCS(PdfDictionary currentColorSpaces, Boolean fill, P
11981208 if (defaultCs .getNumberOfComponents () != numOfComponents )
11991209 throw new PdfAConformanceException (PdfaExceptionMessageConstant .COLOR_SPACE_0_SHALL_HAVE_1_COMPONENTS ).setMessageParams (defaultCsName .getValue (), numOfComponents );
12001210
1201- checkColorSpace (defaultCs , currentColorSpaces , false , fill );
1211+ checkColorSpace (defaultCs , pdfObject , currentColorSpaces , false , fill );
12021212 return true ;
12031213 }
12041214
0 commit comments