Search code examples
c#.netpdfpdfboxdynamicpdf

Identify RGB and CMYK in a PDF file


I know this question has been asked before but it doesn't explain much and as I don't have a reputation to comment there I am asking this question.

The answer that was provided in the aforementioned thread retrieves the r g and b values but I don't know what tells if the values that are found show what part is CMYK (as I understand that after rendering all values are converted into RGB).

I need to first identify what color system is used in a pdf file, I understand now that CMYK and RGB can be simultaneously used in a single file. So I need to analyze the pdf file in my C# application and find a way to convert the CMYK parts to RGB if need be.

I learned that conversion can be done using ABCDpdf.


Solution

  • This is a very broad question and it would be better for you if you read at least part of the PDF specification. To give you a taste of why I'm saying this...

    PDF and color spaces

    1) PDF can contain a wide range of color spaces
    - Device color spaces such as RGB, CMYK and Gray
    - Abstract color spaces such as Lab
    - ICC profile based color spaces such as ICC-based RGB, ICC-based Lab, ...
    - Named or special color spaces such as Separation, Device-N and N-Channel
    (and I'm omitting some charming ones such as patters and shadings)

    2) All of the above color spaces can be used throughout a single PDF file. When your PDF file is compliant to certain ISO standards (such as PDF/A, PDF/X...) it has to obey rules that restrict the number of color spaces, but in general all color spaces are allowed in a single PDF.

    3) Where a PDF file is used determines how these color spaces need to be handled. If you want to print to a desktop printer with CMYK inks, something is going to convert all of these color spaces to CMYK. If you're viewing the PDF file on screen, something is going to convert all of these color spaces to RGB.

    Converting colors

    Yes, you can convert from CMYK (and all of these other color spaces I mentioned) to RGB. But that is also much more difficult that it may sound if you want to do it correctly. As an example have a look at this site: http://www.rapidtables.com/convert/color/cmyk-to-rgb.htm

    It contains quick and easy to use formulas for this conversion:
    R = 255 × (1-C) × (1-K)
    G = 255 × (1-M) × (1-K)
    B = 255 × (1-Y) × (1-K)

    This will work, but in practice you need an engine (such as for example LittleCMS) that uses ICC Profiles (used to characterise color spaces) to do a proper conversion.