Home > Enterprise >  How to fix the itext 7 exception Unexpected ColorSpace
How to fix the itext 7 exception Unexpected ColorSpace

Time:07-06

I try to read a PDF document using the itext 7.1.9 and I get an exception that looks like the following:

com.itextpdf.kernel.pdf.canvas.parser.util.InlineImageParsingUtils$InlineImageParseException: Unexpected ColorSpace: /R9.
    at com.itextpdf.kernel.pdf.canvas.parser.util.InlineImageParsingUtils.getComponentsPerPixel(InlineImageParsingUtils.java:257)
    at com.itextpdf.kernel.pdf.canvas.parser.util.InlineImageParsingUtils.computeBytesPerRow(InlineImageParsingUtils.java:271)
    at com.itextpdf.kernel.pdf.canvas.parser.util.InlineImageParsingUtils.parseUnfilteredSamples(InlineImageParsingUtils.java:298)
    at com.itextpdf.kernel.pdf.canvas.parser.util.InlineImageParsingUtils.parseSamples(InlineImageParsingUtils.java:345)
    at com.itextpdf.kernel.pdf.canvas.parser.util.InlineImageParsingUtils.parse(InlineImageParsingUtils.java:163)
    at com.itextpdf.kernel.pdf.canvas.parser.util.PdfCanvasParser.parse(PdfCanvasParser.java:119)
    at com.itextpdf.kernel.pdf.canvas.parser.PdfCanvasProcessor.processContent(PdfCanvasProcessor.java:283)
    at com.itextpdf.kernel.pdf.canvas.parser.PdfCanvasProcessor.processPageContent(PdfCanvasProcessor.java:306)
    at com.itextpdf.kernel.pdf.canvas.parser.PdfDocumentContentParser.processContent(PdfDocumentContentParser.java:77)
    at com.itextpdf.kernel.pdf.canvas.parser.PdfDocumentContentParser.processContent(PdfDocumentContentParser.java:90)

I suppose this method must be fixed in itext 7 library:

private static int getComponentsPerPixel(PdfName colorSpaceName, PdfDictionary colorSpaceDic) {
        if (colorSpaceName == null) {
            return 1;
        } else if (colorSpaceName.equals(PdfName.DeviceGray)) {
            return 1;
        } else if (colorSpaceName.equals(PdfName.DeviceRGB)) {
            return 3;
        } else if (colorSpaceName.equals(PdfName.DeviceCMYK)) {
            return 4;
        } else {
            if (colorSpaceDic != null) {
                PdfArray colorSpace = colorSpaceDic.getAsArray(colorSpaceName);
                if (colorSpace != null) {
                    if (PdfName.Indexed.equals(colorSpace.getAsName(0))) { // the fail is on here. colorSpace.getAsName(0) returns /ICCBased
                        return 1;
                    }
                } else {
                    PdfName tempName = colorSpaceDic.getAsName(colorSpaceName);
                    if (tempName != null) {
                        return getComponentsPerPixel(tempName, colorSpaceDic);
                    }
                }
            }

            throw (new InlineImageParseException("Unexpected ColorSpace: {0}.")).setMessageParams(new Object[]{colorSpaceName});
        }
    }

I don't understand why I get it and how to fix it.

CodePudding user response:

This issue has been fixed in iText more than a year ago. The fix has first been released in version 7.1.16.

Thus, please update your iText version.


For details, the fix has been added in commit df4013c8f141059a91373008ac4a7013f6be0852 authored on 2021-04-14 10:36:24 and committed on 2021-05-25 15:11:36 with the comment

Add support processing inline image with ICCBased color space in resources

DEVSIX-5295

  • Related