Search code examples
c#pdfitextacrofields

How get acrofield custom attributes with iTextSharp


I have a PDF (vs1 (dopo).pdf) with 6 acrofield ... 4 radio button and 2 signature fields. Signature fields have some custom properties that I would like to read but I don't know how. This is the code I use to retrieve the fields:

PdfReader pdfReader = new PdfReader (inputPDFPath);
AcroFields pdfFormFields = pdfReader.AcroFields;
            
foreach (KeyValuePair <string, AcroFields.Item> kvp in pdfFormFields.Fields)
{
     string fieldName = kvp.Key.ToString ();
     string fieldValue = pdfFormFields.GetField (kvp.Key.ToString ());
     Console.WriteLine (fieldName + "" + fieldValue);
}

pdfReader.Close ();

Signature fields have these properties:

kvp.Value.GetValue(0).Keys

[Count = 12
    [0]: {/F}
    [1]: {/FT}
    [2]: {/Ff}
    [3]: {/MK}
    [4]: {/P}
    [5]: {/Rect}
    [6]: {/SSCbio}
    [7]: {/SSCreq}
    [8]: {/Subtype}
    [9]: {/T}
    [10]: {/sq}
    [11]: {/uid}]

How can I retrieve the value of {/uid}, {/SSCbio}, ...?


Solution

  • You inspected kvp.Value.GetValue(0).Keys, so you nearly already were there! kvp.Value.GetValue(0) is a PdfDictionary you can query the values of:

    foreach (KeyValuePair<string, AcroFields.Item> kvp in pdfFormFields.Fields)
    {
        string fieldName = kvp.Key.ToString();
        string fieldValue = pdfFormFields.GetField(kvp.Key.ToString());
        Console.WriteLine(fieldName + " - " + fieldValue);
        PdfDictionary merged = kvp.Value.GetValue(0);
        foreach (PdfName key in merged.Keys)
        {
            Console.WriteLine("    {0}: {1}", key, merged.GetDirectObject(key));
        }
    }
    

    For the signature fields this returns:

    Firme statiche (1) - 
        /F: 4
        /FT: /Sig
        /Ff: 0
        /MK: Dictionary
        /P: Dictionary of type: /Page
        /Rect: [392.4, 125.016, 505.632, 153.324]
        /SSCbio: 1
        /SSCreq: 1
        /Subtype: /Widget
        /T: Firme statiche (1)
        /sq: -1
        /uid: Firme statiche
    Firme statiche (2) - 
        /F: 4
        /FT: /Sig
        /Ff: 0
        /MK: Dictionary
        /P: Dictionary of type: /Page
        /Rect: [231.12, 87.576, 301.89, 115.884]
        /SSCbio: 1
        /SSCreq: 1
        /Subtype: /Widget
        /T: Firme statiche (2)
        /sq: -1
        /uid: Firme statiche
    

    As an aside, those keys SSCbio, SSCReq, sq, and uid are not defined by the PDF specification. Furthermore, they don't have a registered prefix and no developer extension is declared. Thus, those PDFs should be for internal use in your organisation only and never get out into the wild.

    Also I hope you have established a process to check upon every PDF viewer / signer / validator update relevant in your organisation whether those additions still don't interfere with their operation...