How can I determine if an image in a pdf is grayscale? (2)

Q: I have a requirement to iterate all pages in a document and to
determine whether they are grayscale, colour or Black & White. I have
seen in your knowledgebase/FAQ's the question "How can I determine if
an image in a pdf is grayscale?" which is very similar to what I need
to do.

The problem is that the code snippet supplied in your response(as
below)I cannot get to work in VS2005 using VB.Net.
ColorSpace cs = element.GetImageColorSpace(); ColorSpace.Type type =
cs.GetType(); if (type == ColorSpace.Type....) { }

I cannot seem to return a valid colorspace for the page: the
element.getcolorimagespace() will only return a PDFTRON.SDF.Obj not,
as in your example, a ColorSpace. I have surmised that either my
assigment is incorrect or the example used an earlier incarnation of
the library that has now changed? My attempt below returns a value of
3 for the variable "iscolour" for every page in my document (equating
to colorspace.type enumerated to grayscale). I don't think "iscolour"
is holding the ColorSpace but I cannot seem to use the "pgcolorspace"
in any other manner to get the colorspace.type.

Code snippet below:

          Dim element As Element
          Dim readpage As ElementReader = New ElementReader
          Dim type As Element.Type
          Dim pgcolorspace As PDFTRON.SDF.Obj
          Dim iscolour As Integer
          '**omitted few assignment statements for clarity
          Do While allpages <= numberpages
               pg = doc.GetPage(allpages)
               element = readpage.Next()
               type = element.GetType()
               pgcolorspace = element.GetImageColorSpace()
               iscolour = pgcolorspace.GetType '******this is problem
line below from your example - getimagecolorspace() returns '******a
pdftron.sdf.obj not a colorspace
               'Dim cspace As ColorSpace = element.GetImageColorSpace
                If iscolour = ColorSpace.Type.e_cal_rgb then
******etcetera, etcetera -removed for clarity
                End If
     End Sub
A: The code snippet is referring to a newer version of the API. You
may want to download the latest build of PDFNet where
element.GetImageColorSpace() returns directly ColorSpace instead of

Alternatively you can create ColorSpace from SDF.Obj. For example:

  Dim cspace As ColorSpace = new ColorSpace(element.GetImageColorSpace

Please keep in mind that you can call GetImageColorSpace() on an
element only if you have an image element (i.e. if element.GetType()
is Element.Type.e_image). Also 'readpage.Next()' could possibly return
NULL. You need to call Dispose() en ElementReader when it is no longer
in use, etc.

If you are determined to use ElementReader approach, the best starting
point is to take a look at ElementReaderAdv sample project (http:// and copy & paste the relevant
code that you need. This will remove lots of guesswork about proper
API usage.

Again please keep in mind that this way to determining if a PDF page
contains color information can be tricky and requires lots of
knowledge about PDF format. A much simple approach to determine
whether a page grayscale is to render a small(?) PDF bitmap (e.g.
using PDFDraw.GetBitmap()) and then check the image for any pixels
where R, G, and B values do not match.