How do I check if all signature fields on a PDF form have been signed?

Q:

We need a quick check that all signature fields on a PDF form have been signed. Is this something that can done via the PDFtron SDK?

A:

In check if all signature fields have been signed, you will need to iterate through the list of fields using PDFDoc.GetFieldIterator (or if you are interested in only a given page iterate through page annotations as shown in Annotation sample, filtering Widget annotations with subtype e_signature). As you go through each field, you can filter them by the type (i.e. Field.Type.e_signature). Once you hit a signature field, you can check whether this field contains a “/V” key. You can do this by checking Field.GetValue() != null. A sample pseudo code would be the following:

using (PDFDoc doc = new PDFDoc(“MyDoc.pdf”)) {
var fieldItr = doc.GetFieldIterator();
for ( ; fieldItr.HasNext(); ) {
var field = itr.Next();
if (field.GetType == Field.Type.e_signature) {
if (field.GetValue() == null) {
// One of the signature fields does not have a signature.
}
}
}
}

I have tried this but whether or not the field is signed I get a non-null value returned from the getValue() call. What can be the issue here?

It is possible that it returns an empty dictionary. If such is the case, then field.GetValue() will not return null.

To check for these kinds of situations, you will need to slightly modify the code:

using (PDFDoc doc = new PDFDoc(“MyDoc.pdf”)) {
var fieldItr = doc.GetFieldIterator();
for ( ; fieldItr.HasNext(); ) {
var field = itr.Next();
if (field.GetType == Field.Type.e_signature) {
if (field.GetValue() == null) {
// One of the signature fields does not have a signature.
}
else {

// Check if the value contains an entry for /ByteRange. The /ByteRange key is necessary for adding digital signatures and is a required field.

var valueObj = field.GetValue();

if (valueObj.IsDict()) { // The value must be a dictionary object.

if (valueObj.Find(“Contents”) != null) {

// This field is signed.

}

}

// Alternatively, you can check for the existence of /ByteRange and /Filter keys.

// Please see PDF Reference for more information.
}

}
}
}