tags:

views:

273

answers:

3

Given a PDF file with color and black & white pages, is there any way with C# to find out among the given pages which are color and which are black & white?

A: 

Short of parsing all the postscript content, probably not. There's no flag on a PDF page that says it is or is not b&w or color. So you'd have to check the color of every element placed on the page to figure out if it was color or not. I'm not sure what libraries exist for reading PDFs on C# but you would need one that will read all the elements.

Similarly, any images you have on the page would need to be checked for color and that is not simple. Color image formats can hold b&w images.

jmucchiello
Check out http://csharp-source.net/open-source/pdf-libraries Any number of these should be up to the task.
codeelegance
My point is you need to write a lot of code to read through the PDF. There are issues involving images, recurring headers and footers, etc.
jmucchiello
Thanks for your replyI found that there are some color directives in PDF while searcing the net, but i could not understand that do you have any idea regarding the same?
I may be wrong, but PDFs are based on postscript. PS is a stack based language that draws stuff on "pages". Directives in PS can jump to page locations, draw lines, circles and other shapes and can output strings of text in the current font face/size/color. Between all these actions, the color of the drawing implement can be altered. Text on the page does not need to drawn in any particular order. This means you need to parse the whole page looking for certain commands that involve color. You should probably look for a tutorial about PS to understand PDF construction at the level you need.
jmucchiello
+1  A: 

My recommendation is to render each page to an image and then check each pixel for RGB values not equal to each other. If R=G=B for each pixel then it's a grayscale image.

You could then perform actions (such as extracting a page to another document or printing the page) on the pages based on whether they are color pages or black and white pages, etc.

This can be achieved by using my companies PDF developer library, Quick PDF, or potentially by one of the open source PDF libraries that Kenneth suggested.

Rowan
A: 

Check out:

PDF-Analyser

I use his tools for text extraction and pdf analysis. Very inexpensive, royalty free, and work well. I think GetPDFColourStyle as part of the PDFLayoutPlus library should do the trick.

Douglas Anderson