1
votes

While extracting color from PDF using iTextSharp I get this error :

int r = renderInfo.GetColorNonStroke().R;
int g = renderInfo.GetColorNonStroke().G;
int b = renderInfo.GetColorNonStroke().B;

Error message :

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception details:

System.NullReferenceException: Object reference not set to an instance of an object.

1
Either renderInfo is null of GetColorNonStroke is null. - plinth
The message is telling you that one of the objects you are trying to access is null. - George Johnston
Check the debugger before you reach those lines, and verify that renderInfo is not null - Jonesopolis
render info is not null. the pdf text font-color is black. i tested before few pdf which were having font color black and took r g b value has #000000 but in this its not asking and i am getting error . can you please help is there any other things. should include any other things . or my code is missing to find any color . thankyou in advance - pdp
Black could also be a Graycolor with intensity 0. Why do you think it's RGB? - Bruno Lowagie

1 Answers

2
votes

As GetColorNonStroke is not a method of the original iTextSharp TextRenderInfo, I assume you are using a version patched according to @ChrisHaas' blog entry Getting color information from iTextSharp’s TextRenderInfo and ITextExtractionStrategy.

The members colorStroke and colorNonStroke of GraphicsState (the values of which are returned by the TextRenderInfo methods GetColorStroke and GetColorNonStroke respectively) are initialized with null.

Thus, as long as there was no explicit command before to set the stroking or non-stroking color, the respective TextRenderInfo method GetColorStroke or GetColorNonStroke will return null.

Therefore, whenever you use renderInfo.GetColorNonStroke(), check it for null before accessing its members. If the color is null, assume the default.

Furthermore, @ChrisHaas' color extension of iTextSharp's parsing capabilities uses different kinds of color objects: GrayColor, BaseColor, and CMYKColor. Depending on your requirements you might want to test the type of color returned to you.

In case of special colors, Chris Haas even writes:

SCN and scn themselves are catchalls for everything else that’s not RGB, CMYK or Grey. Before hitting one of those two you should actually first find a CS operator whose first and only operand is the actual color space to use. There’s a bunch of options for this including DeviceRGB, DeviceCMYK, Pattern, Lab, DeviceN, etc. You can find these in table 74 of the 2008 PDF spec section 8.6.8 (page 171). My code is actually not completely correct and I shouldn’t be pushing CS and cs to the SetStrokingGeneral method but instead should do some further processing. Unfortunately none of the samples PDFs that I had at the time had this set so I couldn’t test for it. Hopefully this helps you out!