1

I'm trying to create a visible PDF signature pragmatically. Non-visible signature works but when I add the AP (appearance) object I get an error in Acrobat Reader saying the following: 'Object label badly formatted'.

The relevant XObject that I make look like this:

38 0 obj
<</F 132/Type/Annot/Subtype/Widget/Rect[343.0 142.0 543.0 192.0]/FT/Sig/DR<<>>/T(signature38)/V 40 0 R/AP <</N 41 0 R>>>>
endobj

39 0 obj << /Length 52 /Type /XObject /Subtype /Form /Resources << /XObject << /n2 42 0 R /n0 43 0 R >> /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] >> /BBox [0.0 0.0 200.0 50.0] /FormType 1 >> stream q 1 0 0 1 0 0 cm /n0 Do Q q 1 0 0 1 0 0 cm /n2 Do Q endstream endobj
40 0 obj
<</Contents <...>
/Type/Sig/SubFilter/adbe.pkcs7.detached/Location()/M(D:19700101010000+01'00')
/ByteRange [0000000000 0000064440 0000068536 0000220176]/Filter/Adobe.PPKLite/Reason()/ContactInfo()>>
endobj

41 0 obj << /Length 27 /Type /XObject /Subtype /Form /Resources << /XObject << /FRM 39 0 R >> /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] >> /BBox [0.0 0.0 200.0 50.0] /FormType 1 >> stream q 1 0 0 1 0 0 cm /FRM Do Q endstream 
endobj
42 0 obj << /Length 31 /Type /XObject /Subtype /Form /BBox [0.0 0.0 200.0 50.0] /Matrix [1.0 0.0 0.0 1.0 0.0 0.0] /Resources << /XObject << /img1 44 0 R >> /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] >> /FormType 1 >> 
stream q 200 0 0 50 0 0 cm /img1 Do Q 
endstream 
endobj
43 0 obj << /Length 0 /Type /XObject /Subtype /Form /BBox [0.0 0.0 200.0 50.0] /Resources << >> /FormType 1 >> stream endstream endobj
44 0 obj << /Length 218095 /Type /XObject /Subtype /Image /Filter /DCTDecode /BitsPerComponent 8 /Width 1300 /Height 1261 /ColorSpace /DeviceRGB 
stream
-JPEG bytes-
endstream

I can't seem to find the error in this, but I must be missing something. I read this whole document by Adobe along with some other blogs and made my code according to that.

**UPDATE: **

I changed the code according to mkl's comment:

    38 0 obj
<</F 132/Type/Annot/Subtype/Widget/Rect[343.0 142.0 543.0 192.0]/FT/Sig/DR<<>>/T(signature38)/V 40 0 R/P 3 0 R/AP <</N 41 0 R>>>>
endobj

39 0 obj
<<
/Length 52
/Type /XObject
/Subtype /Form
/Resources <<
/XObject <<
/n2 42 0 R
/n0 43 0 R
>>
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
>>
/BBox [0.0 0.0 200.0 50.0]
/FormType 1
>>
stream
q 1 0 0 1 0 0 cm /n0 Do Q q 1 0 0 1 0 0 cm /n2 Do Q

endstream
endobj
40 0 obj
<<content>>
endobj

41 0 obj
<<
/Length 27
/Type /XObject
/Subtype /Form
/Resources <<
/XObject <<
/FRM 39 0 R
>>
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
>>
/BBox [0.0 0.0 200.0 50.0]
/FormType 1
>>
stream
q 1 0 0 1 0 0 cm /FRM Do Q

endstream
endobj
42 0 obj
<<
/Length 31
/Type /XObject
/Subtype /Form
/BBox [0.0 0.0 200.0 50.0]
/Matrix [1.0 0.0 0.0 1.0 0.0 0.0]
/Resources <<
/XObject <<
/img1 44 0 R
>>
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
>>
/FormType 1
>>
stream
q 200 0 0 50 0 0 cm /img1 Do Q

endstream
endobj
43 0 obj
<<
/Length 0
/Type /XObject
/Subtype /Form
/BBox [0.0 0.0 200.0 50.0]
/Resources <<
>>
/FormType 1
>>
stream

endstream
endobj
44 0 obj << /Length 9451 /Type /XObject /Subtype /Image /Filter /DCTDecode /BitsPerComponent 8 /Width 1300 /Height 1261 /ColorSpace /DeviceRGB 
stream
--JPEG bytes--
endstream

Here you can download my PDF.

UPDATE 2: It seems there was an error with my xref tables, where they weren't pointing to the right XObjects, that's why I was getting the error. However, now even though my PDF isn't corrupted and signature is ok (adobe will tell that the signature can't be verified because the certificate is self-signed but that's ok).

But now, I have another problem: The image doesn't appear. It's clickable and works but it's all blank.

A new version of the PDF is available here.

user3362334
  • 1,980
  • 3
  • 26
  • 58
  • First of all your streams mostly are invalid. *The keyword **stream** shall be followed by an end-of-line marker*. For further analysis please share the whole example pdf, and please do so as a binary download (e.g. via a public dropbox or Google drive share) as posting pdfs as text changes them. – mkl Sep 14 '19 at 07:33
  • That being said, please keep to reading the pdf specification, not some Adobe documents from the times when there was no pdf specification. The document you have essentially shows how Adobe software structures a signature widget appearance which is more complicated than what actually is required. – mkl Sep 14 '19 at 07:36
  • By the way, you tagged your question [tag:pades] but use the **SubFilter** value **adbe.pkcs7.detached**. That is weird, if one starts implementing PAdES signing, why would one use the legacy profile? – mkl Sep 14 '19 at 07:49
  • @mkl 1 - I fixed the end-of-line problems, see my update. I also uploaded the PDF, 2 - I wrote the code in javascript at a really low level so I needed a deeper understanding of PDF because I'm adding theese XObjects manually 3 - yes I should change the filter to pAdES – user3362334 Sep 15 '19 at 01:44
  • I'll look at the files, most likely not before Monday, though. – mkl Sep 15 '19 at 06:21

1 Answers1

1

The issues you have come across are typical for projects implementing specific tasks of PDF editing without using a tested PDF library.

As discussed in comments, in the first file of which you only posted an excerpt there were fundamental syntax errors, in particular ignoring that for streams the specification requires The keyword stream shall be followed by an end-of-line marker.

Fixing that in your second file you yourself found broken cross reference entries.

Now you discuss the third file which appears to be properly signed as viewed in Adobe Reader, merely the image doesn't show up.

Actually, though, there still are multiple errors.

One reason for the image not showing up is that the image XObject stream is syntactically broken:

44 0 obj << /Length 9451 /Type /XObject /Subtype /Image /Filter /DCTDecode /BitsPerComponent 8 /Width 1300 /Height 1261 /ColorSpace /DeviceRGB 
stream 
...

Without looking into the values you set in the dictionary part of the stream, one already sees that the closing >> of that dictionary is missing. Thus, a PDF processor does not see where the image metadata ends and where the image payload starts.

In hindsight that was already visible in the previous files you posted, but there other issues first hit the eye.

Furthermore your cross references still are somewhat broken, many entries point to file offset 23 with some arbitrary generation number. At least some of the object numbers so treated are referenced in the document. This makes some programs already fail loading your PDF.

Also already discussed in comments, you tagged your question but use the SubFilter value adbe.pkcs7.detached. That is weird, if one starts implementing PAdES signing, why would one use the legacy profile?

mkl
  • 90,588
  • 15
  • 125
  • 265
  • I can't believe I missed the closing >>. However, now when I added it I get the same result. – user3362334 Sep 17 '19 at 00:21
  • Please share the current state of your pdf. But please also repair the cross references, some tools for analysis could not parse the previous version because if the cross reference issues. – mkl Sep 17 '19 at 04:55