pdfacrobatpdfium

Pdfium library : Get the comments on page


How to get comment made on pdf page using acrobat reader with pdfium. Got 2 annotations from FPDFPage_GetAnnot method like HIGHLIGHT,POPUP But not able to figure out the comments text.

[
    {
        "rect": {
            "left": 69.9908,
            "top": 810.952,
            "right": 139.376,
            "bottom": 795.96
        },
        "sub_type": "HIGHLIGHT",
        "objects": [
            {
                "type": "FORM",
                "rect": {
                    "left": 0.4410019,
                    "top": 14.550964,
                    "right": 68.9445,
                    "bottom": 0.44110107
                },
                "stroke_color": {
                    "r": 0,
                    "g": 0,
                    "b": 0,
                    "a": 29
                },
                "fill_color": {
                    "r": 0,
                    "g": 0,
                    "b": 0,
                    "a": 29
                },
                "stroke_width": 1,
            }
        ]
    },
    {
        "rect": {
            "left": 532.14795,
            "top": 810.47205,
            "right": 716.73004,
            "bottom": 690.03503
        },
        "sub_type": "POPUP",
        "color": {
            "r": 0,
            "g": 0,
            "b": 0,
            "a": 255
        },
        "has_attachment_points": false,
        "flags": "NONE",
    }
]

enter image description here

Tried with multiple functions from pdfium library.


Solution

  • @K J your answer was so much helpful to understand the underlying pdf obj and annotations. With respect to Pdfium library able extract all the comments and State.

    using Pdfium first got the linked Annotation, following is in go-lang.

    getLinkedAnnot, _ := instance.FPDFAnnot_GetLinkedAnnot(&requests.FPDFAnnot_GetLinkedAnnot{Annotation: annotation, Key: "Popup"})
     if getLinkedAnnot != nil {
      getLinkedAnnot.LinkedAnnotation   
     }      
    

    Then also getting the values of all key

    keys := []string{"Contents", "CreationDate", "Subj", "State", "RC", "T"}
        var params []*common.Param
        for i := 0; i < len(keys); i++ {
            key := keys[i]
            hasKey, err := instance.FPDFAnnot_HasKey(&requests.FPDFAnnot_HasKey{Annotation: annotation, Key: key})
            if err != nil {
                return nil, err
            }
            if hasKey.HasKey {
                var param = &common.Param{Key: key}
                valueType, err := instance.FPDFAnnot_GetValueType(&requests.FPDFAnnot_GetValueType{Annotation: annotation, Key: key})
                if err != nil {
                    return nil, err
                }
                param.Type = common.GetType(valueType.ValueType)
                if valueType.ValueType == enums.FPDF_OBJECT_TYPE_STRING {
                    value, err := instance.FPDFAnnot_GetStringValue(&requests.FPDFAnnot_GetStringValue{Annotation: annotation, Key: key})
                    if err != nil {
                        return nil, err
                    }
                    param.Value = value.Value
                } else if valueType.ValueType == enums.FPDF_OBJECT_TYPE_NUMBER {
                    value, err := instance.FPDFAnnot_GetNumberValue(&requests.FPDFAnnot_GetNumberValue{Annotation: annotation, Key: key})
                    if err != nil {
                        return nil, err
                    }
                    param.Value = fmt.Sprintf("%f", value.Value)
                }
                params = append(params, param)
            }
        }
    

    which gives

    "params": [
                            {
                                "key": "Contents",
                                "type": "STRING",
                                "value": "Accepted set by omprakashseervi"
                            },
                            {
                                "key": "CreationDate",
                                "type": "STRING",
                                "value": "D:20240320170027+05'30'"
                            },
                            {
                                "key": "Subj",
                                "type": "STRING",
                                "value": "Sticky Note"
                            },
                            {
                                "key": "State",
                                "type": "STRING",
                                "value": "Accepted"
                            },
                            {
                                "key": "RC",
                                "type": "STRING",
                                "value": "<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"Acrobat:23.8.0\" xfa:spec=\"2.0.2\" ><p>Accepted set by omprakashseervi</p></body>"
                            },
                            {
                                "key": "T",
                                "type": "STRING",
                                "value": "omprakashseervi"
                            }
                        ]