diff --git a/src/PDFDoc.php b/src/PDFDoc.php index 2ce3550..b87d3ef 100644 --- a/src/PDFDoc.php +++ b/src/PDFDoc.php @@ -70,6 +70,7 @@ class PDFDoc extends Buffer { protected $_certificate = null; protected $_signature_ltv_data = null; protected $_signature_tsa = null; + protected $_signature_annotation_object_id = null; protected $_appearance = null; protected $_xref_table_version; protected $_revisions; @@ -258,6 +259,40 @@ public function get_object($oid, $original_version = false) { return $object; } + /** + * Function that sets the signature annotation object that will be used to sign the document. If this is not set, + * there will be a new signature annotation object created. + * @param int $oid The object ID of the signature annotation object + * @return bool True if the object ID is a signature annotation object, false otherwise + */ + public function set_signature_annotation_object_id(int $oid) { + $pdfObject = $this->get_object($oid); + if (false === $pdfObject) { + return p_error('Signature box with ID: '.$oid.' not found'); + } + + $value = $pdfObject->get_value()->val(); + + if (!is_array($value)) { + return p_error('Object ID: '.$oid.' does not have a value'); + } + + if (!isset($value['Type']) || 'Annot' !== $value['Type']->val()) { + return p_error('Object ID: '.$oid.' has invalid type value'); + } + + if (!isset($value['Subtype']) || 'Widget' !== $value['Subtype']->val()) { + return p_error('Object ID: '.$oid.' has invalid subtype value'); + } + + if (!isset($value['FT']) || 'Sig' !== $value['FT']->val()) { + return p_error('Object ID: '.$oid.' has invalid FT value'); + } + + $this->_signature_annotation_object_id = $oid; + return true; + } + /** * Function that sets the appearance of the signature (if the document is to be signed). At this time, it is possible to set * the page in which the signature will appear, the rectangle, and an image that will be shown in the signature form. @@ -443,17 +478,21 @@ protected function _generate_signature_in_document() { } // Create the annotation object, annotate the offset and append the object - $annotation_object = $this->create_object([ - "Type" => "/Annot", - "Subtype" => "/Widget", - "FT" => "/Sig", - "V" => new PDFValueString(""), - "T" => new PDFValueString('Signature' . get_random_string()), - "P" => new PDFValueReference($page_obj->get_oid()), - "Rect" => $recttoappear, - "F" => 132 // TODO: check this value - ] - ); + if (null === $this->_signature_annotation_object_id) { + $annotation_object = $this->create_object([ + "Type" => "/Annot", + "Subtype" => "/Widget", + "FT" => "/Sig", + "V" => new PDFValueString(""), + "T" => new PDFValueString('Signature' . get_random_string()), + "P" => new PDFValueReference($page_obj->get_oid()), + "Rect" => $recttoappear, + "F" => 132 // TODO: check this value + ] + ); + } else { + $annotation_object = $this->get_object($this->_signature_annotation_object_id); + } // Prepare the signature object (we need references to it) $signature = null; @@ -562,6 +601,11 @@ protected function _generate_signature_in_document() { $annotation_object["Rect"] = [ $recttoappear[0], $pagesize_h - $recttoappear[1], $recttoappear[2], $pagesize_h - $recttoappear[3] ]; } + if (null !== $this->_signature_annotation_object_id) { + $this->add_object($annotation_object); + return $signature; + } + if (!$newannots->push(new PDFValueReference($annotation_object->get_oid()))) return p_error("Could not update the page where the signature has to appear");