0

I am signing PDF programmatically. Every new signature is added in an incremental way where I add the signature dictionnary after the %EOF and I update the AcroForm like this (sorry, I'm at work so I can't upload the PDF) :

... // ORIGINAL FILE

trailer
<<
/Size 11
/Root 1 0 R
/Info 10 0 R
>>

startxref
2714
%%EOF
1 0 obj 
<</Type /Catalog /Outlines 2 0 R /Pages 3 0 R /AcroForm <</Fields [11 0 R ] /SigFlags 3 >> /Names 14 0 R >>
endobj

...

11 0 obj 
<</Type /Annot /SubType /Widget /Rect [0 0 0 0 ] /P 4 0 R /F 4 /FT /Sig /T (Signature) /Ff 0 /V <</Type /Sig /Filter /Adobe.PPKLite /SubFilter /adbe.pkcs7.detached /ByteRange [0 3729 15473 422                   ] /Contents <308209...> 
/M (D:20170801165520+02'00') >> >>
endobj

...

trailer
<</Size 15 /Root 1 0 R /Info 10 0 R /Prev 2714 >>
startxref
15609
%%EOF
1 0 obj 
<</Type /Catalog /Outlines 2 0 R /Pages 3 0 R /AcroForm <</Fields [15 0 R ] /SigFlags 3 >> /Names 14 0 R >>
endobj

...

15 0 obj 
<</Type /Annot /SubType /Widget /Rect [0 0 0 0 ] /P 4 0 R /F 4 /FT /Sig /T (Signature) /Ff 0 /V <</Type /Sig /Filter /Adobe.PPKLite /SubFilter /adbe.pkcs7.detached /ByteRange [0 16632 28376 387                  ] /Contents <3082062...> 
/M (D:20170802094848+02'00') >> >>
endobj
16 0 obj 

...

<</Size 18 /Root 1 0 R /Info 10 0 R /Prev 15609 >>
startxref
28476
%%EOF

Maybe the problem is that I have multiple objects having the same ID and that my last AcroForm only refers to the last signature ? I want to be able to sign one file multiple times but I have an issue. The first signing is okay and displays this banner :

enter image description here

And then I try to sign the same file another time with another certificate and this give me this trash can icon next to the signature and says it was removed as you can see here :

enter image description here

I am using Zend_PDF to parse the file and add the signature.

EDIT : Thanks to @mkl the 2 signatures are now recognized by Adobe Reader. My AcroForms now look like this :

2714
%%EOF
1 0 obj 
<</Type /Catalog /Outlines 2 0 R /Pages 3 0 R /AcroForm <</Fields [11 0 R ] /SigFlags 3 >> /Names 14 0 R >>
endobj

... 

15610
%%EOF
1 0 obj 
<</Type /Catalog /Outlines 2 0 R /Pages 3 0 R /AcroForm <</Fields [11 0 R 15 0 R ] /SigFlags 3 >> /Names 14 0 R >>
endobj

...

And I have this surprising errors since I didn't modify anything about the ByteRange and I verified by hand that they are correct. Is is about my new modifications ?

enter image description here

Shashimee
  • 256
  • 3
  • 20
  • Concerning the edit: As you see in your screenshots, *the signature byte ranges are invalid*. Check them! As long as you don't share the PDFs in question in binary form, we cannot help you with that. – mkl Aug 02 '17 at 10:57

1 Answers1

1

While adding the first signature you set the AcroForm dictionary to

/AcroForm <</Fields [11 0 R ] /SigFlags 3 >> 

For the second one, you set it to

/AcroForm <</Fields [15 0 R ] /SigFlags 3 >>

I.e. you removed 11 0 R from it and added 15 0 R to it. Thus, you indeed deleted the first signature field from the form structure.

You should instead only have added the new signature field:

/AcroForm <</Fields [11 0 R 15 0 R ] /SigFlags 3 >>

Furthermore, both your signature fields have the same name

11 0 obj 
<<
    ...
    /T (Signature) 
    ...
>>
endobj
...
15 0 obj 
<<
    ...
    /T (Signature)
    ...
>>
endobj

Obviously, different form fields must have different names.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • thanks for the quick answer ! I updated my process with the information you gave me and updated my question since I'm facing a new problem. – Shashimee Aug 02 '17 at 10:09
  • *updated my question since I'm facing a new problem* - as soon as the original question is answered appropriately, one usually considers the question finished, accepting the answer, and the asks a new question for new problems. – mkl Aug 02 '17 at 11:00
  • since I'm going to provide the same code, could you just tell me (if you know) if the error is from the new update ? If people have the same two errors consecutively it might be part of the same answer. – Shashimee Aug 02 '17 at 12:06
  • @Shashimee *"could you just tell me (if you know)..."* - unfortunately I cannot tell. You've only presented small excerpts of your PDFs yet, at most (in your former question) a complete one in text form which made it unusable for signature related analysis. Fortunately for this question, you chose the exact excerpts required for my answer. To analyze the new issue, I would need a sample transferred in binary form. – mkl Aug 02 '17 at 12:28
  • I see thanks for the answers you helped me a lot ! :) I'll write a new question – Shashimee Aug 02 '17 at 12:29