I tried the code in the link you posted and it worked for me. The colors I get back are 148.92, 179.01001 and 214.965. I wish I could give you my PDF to work with, maybe if I store it externally to SO? My PDF used a sort of palish blue color and that seems to match. It was just one page of text created in Word 2010 and exported, nothing too intense.
A couple of suggestions ....
- Recall that the value returned is a float between 0 and 1. If a value is accidentally cast to int, then of course the values will end up containing nearly all 0. The linked to code multiples by 255 to get a range of 0 to 255.
- As the commenter said, the most common color for a PDF file is black which is 0 0 0
That is all I can think of now, otherwise I have version of 1.7.1 of pdfbox and fontbox and like I said I pretty much followed the link you gave.
EDIT
Based upon my comments, here perhaps is a minorly invasive way of doing it for pdf files like color.pdf
?
In PDFStreamEngine.java
in the processOperator
method one can do inside the try block
if (operation.equals("RG")) {
// stroking color space
System.out.println(operation);
System.out.println(arguments);
} else if (operation.equals("rg")) {
// non-stroking color space
System.out.println(operation);
System.out.println(arguments);
} else if (operation.equals("BT")) {
System.out.println(operation);
} else if (operation.equals("ET")) {
System.out.println(operation);
}
This will show you the information, then it is up to you to process the color information for each section according to your needs. Here is a snippet from the beginning of the output of the above code when run on color.pdf
...
BT
rG
[COSInt(1), COSInt(0), CosInt(0)]
RG
[COSInt(1), COSInt(0), CosInt(0)]
ET
BT
ET
BT
rG
[COSFloat{0.573}, COSFloat{0.816}, COSFloat{0.314}]
RG
[COSFloat{0.573}, COSFloat{0.816}, COSFloat{0.314}]
ET
......
You see in the above output an empty BT ET section, this being a section which is marked DEVICEGRAY. All the other give you [0,1] values for the R, G and B components
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…