Search code examples
pythonpdfannotationspoppler

Using poppler to extract annotations. g_free() / get_color() issue


I borrowed this python code here (first answer by enno groper) to automate the extraction of annotations from pdfs.

I want to make some modifications to the code. Trying to fetch the color of annotations with annot_mapping.annot.get_color() I ran into the first issue. What the command returns is objects like this one <PopplerColor at 0x1a85180>, rather than rgb values (promised here).

According to poppler docs poppler_annot_get_color() returns "a new allocated PopplerColor with the color values of poppler_annot , or NULL. It must be freed with g_free() when done".

Is this correct and if yes, how do I achieve this in python?


Solution

  • annot_mapping.annot.get_color() gives you a PopplerColor, which is a structure with three members (of type guint16): red, green, and blue. For example:

    PopplerColor *color = poppler_annot_get_color (annot);
    g_printf ("%d\n", color->red);
    

    gives you the red value of your annotation annot, the gb values can be obtained similarly.

    In python this is achieved through annot_mapping.annot.get_color().red, assuming you have import poppler