ItGo.me Focus on IT Recommend

Home > ios - I have inserted an image into pdf digital signature layer n2 using mupdf, but it not showed up

ios - I have inserted an image into pdf digital signature layer n2 using mupdf, but it not showed up

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

I am using mupdf(git clone at commit 2014/3/5 4:05) to sign a pdf.

And I've already succeeded to sign a digital signature in pdf with function 'pdf_sign_signature'.

Now I'm trying to insert an image into layer n2 of that signature when I'm signing it.

There's a function called 'pdf_set_signature_appearance' in '/source/pdf/pdf-appearance.c'.

I try to add codes below to insert the image:

image_doc = (image_document *)fz_open_document(ctx, jpgFilePathAndName);
fz_fill_image(dev, image_doc->image, page_ctm, 1.0f);

these codes are inserted between:

rect = annot->rect;
rect.x0 = (rect.x0 + rect.x1)/2.0f;
text = fit_text(ctx, &font_rec, (char *)bufstr, &rect);
fz_fill_text(dev, text, page_ctm, cs, font_rec.da_rec.col, 1.0f);

and

rect = annot->rect;
fz_transform_rect(&rect, page_ctm);
pdf_set_annot_appearance(doc, annot, &rect, dlist);

After executing, I got a pdf file like this:

%PDF-1.4
%μῦ

1 0 obj
<<
  /Type /Font
  /BaseFont /Helvetica
  /Subtype /Type1
  /Encoding /WinAnsiEncoding
>>
endobj

2 0 obj
<<
  /Length 63
>>
stream
q
BT
36 806 Td
0 -18 Td
/F1 12 Tf
(Hello World!)Tj
0 0 Td
ET
Q
endstream
endobj

3 0 obj
<<
  /Count 1
  /Type /Pages
  /ITXT (5.3.0)
  /Kids [ 4 0 R ]
>>
endobj

4 0 obj
<<
  /Annots 10 0 R
  /Contents 2 0 R
  /MediaBox [ 0 0 595 842 ]
  /Parent 3 0 R
  /Resources <<
    /Font <<
      /F1 1 0 R
    >>
    /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
  >>
  /Type /Page
>>
endobj

5 0 obj
<<
  /AcroForm 8 0 R
  /Metadata 9 0 R
  /Pages 3 0 R
  /Type /Catalog
>>
endobj

6 0 obj
<<
  /CreationDate (D:20120620091727+02'00')
  /ModDate (D:20120804153425+02'00')
  /Producer (iText\256 5.3.0 \2512000-2012 1T3XT BVBA)
>>
endobj

7 0 obj
<<
  /AP <<
    /N 17 0 R
  >>
  /DA (/Helv 0 Tf 0 g)
  /F 4
  /FT /Sig
  /MK <<
  >>
  /P 4 0 R
  /Rect [ 41.3670998 713.710022 237.352997 781.257996 ]
  /Subtype /Widget
  /T (Signature1)
  /Type /Annot
  /V 15 0 R
>>
endobj

8 0 obj
<<
  /DA (/Helv 0 Tf 0 g )
  /DR <<
    /Encoding <<
      /PDFDocEncoding 14 0 R
    >>
    /Font <<
      /Helv 12 0 R
      /ZaDb 13 0 R
    >>
    /XObject <<
      /DSz 11 0 R
    >>
  >>
  /Fields [ 7 0 R ]
  /SigFlags 1
>>
endobj

9 0 obj
<<
  /Length 3304
  /Subtype /XML
  /Type /Metadata
>>
stream
<?xpacket begin="Ôªø" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c043 52.372728, 2009/01/18-15:08:04        ">
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <rdf:Description rdf:about=""
            xmlns:xmp="http://ns.adobe.com/xap/1.0/">
         <xmp:CreateDate>2012-06-20T09:17:27+02:00</xmp:CreateDate>
         <xmp:ModifyDate>2012-08-04T15:34:25+02:00</xmp:ModifyDate>
         <xmp:MetadataDate>2012-08-04T15:34:25+02:00</xmp:MetadataDate>
      </rdf:Description>
      <rdf:Description rdf:about=""
            xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
         <pdf:Producer>iText® 5.3.0 ©2000-2012 1T3XT BVBA</pdf:Producer>
      </rdf:Description>
      <rdf:Description rdf:about=""
            xmlns:dc="http://purl.org/dc/elements/1.1/">
         <dc:format>application/pdf</dc:format>
      </rdf:Description>
      <rdf:Description rdf:about=""
            xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">
         <xmpMM:DocumentID>uuid:547fb234-f327-4f3d-8dd1-7b7ea87df336</xmpMM:DocumentID>
         <xmpMM:InstanceID>uuid:72437fbf-7b45-4062-98f1-2b53d3ad86d0</xmpMM:InstanceID>
      </rdf:Description>
   </rdf:RDF>
</x:xmpmeta>





















<?xpacket end="w"?>endstream
endobj

10 0 obj
[ 7 0 R ]
endobj

11 0 obj
<<
  /BBox [ 41.3670998 713.710022 237.352997 781.257996 ]
  /Length 67
  /Resources <<
    /XObject <<
      /Img0 16 0 R
    >>
  >>
  /Subtype /Form
  /Type /XObject
  /Matrix [ 1 0 0 1 0 0 ]
>>
stream
1.000000 0.000000 0.000000 -1.000000 0.000000 1.000000 cm
/Img0 Do
endstream
endobj

12 0 obj
<<
  /BaseFont /Helvetica
  /Encoding 14 0 R
  /Name /Helv
  /Subtype /Type1
  /Type /Font
>>
endobj

13 0 obj
<<
  /BaseFont /ZapfDingbats
  /Name /ZaDb
  /Subtype /Type1
  /Type /Font
>>
endobj

14 0 obj
<<
  /Differences [ 24 /breve /caron /circumflex /dotaccent /hungarumlaut 
    /ogonek /ring /tilde 39 /quotesingle 96 /grave 128 /bullet 
    /dagger /daggerdbl /ellipsis /emdash /endash /florin /fraction 
    /guilsinglleft /guilsinglright /minus /perthousand /quotedblbase 
    /quotedblleft /quotedblright /quoteleft /quoteright /quotesinglbase 
    /trademark /fi /fl /Lslash /OE /Scaron /Ydieresis /Zcaron 
    /dotlessi /lslash /oe /scaron /zcaron 160 /Euro 164 /currency 
    166 /brokenbar 168 /dieresis /copyright /ordfeminine 172 
    /logicalnot /.notdef /registered /macron /degree /plusminus 
    /twosuperior /threesuperior /acute /mu 183 /periodcentered 
    /cedilla /onesuperior /ordmasculine 188 /onequarter /onehalf 
    /threequarters 192 /Agrave /Aacute /Acircumflex /Atilde /Adieresis 
    /Aring /AE /Ccedilla /Egrave /Eacute /Ecircumflex /Edieresis 
    /Igrave /Iacute /Icircumflex /Idieresis /Eth /Ntilde /Ograve 
    /Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash 
    /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls 
    /agrave /aacute /acircumflex /atilde /adieresis /aring /ae 
    /ccedilla /egrave /eacute /ecircumflex /edieresis /igrave 
    /iacute /icircumflex /idieresis /eth /ntilde /ograve /oacute 
    /ocircumflex /otilde /odieresis /divide /oslash /ugrave /uacute 
    /ucircumflex /udieresis /yacute /thorn /ydieresis ]
  /Type /Encoding
>>
endobj

15 0 obj
<<
  /ByteRange[0 6712 10814 12756]                               /Contents 
...
  /Filter /Adobe.PPKLite
  /SubFilter /adbe.pkcs7.detached
>>
endobj

16 0 obj
<<
  /Type /XObject
  /Subtype /Image
  /Width 400
  /Height 332
  /ColorSpace /DeviceRGB
  /BitsPerComponent 8
  /Length 11097
>>
stream
...
endstream
endobj

17 0 obj
<<
  /BBox [ 41.3670998 713.710022 237.352997 781.257996 ]
  /FormType 1
  /Length 7
  /Matrix [ 1 0 0 1 0 0 ]
  /Resources <<
    /ProcSet [ /PDF /Text ]
    /XObject <<
      /FRM 18 0 R
    >>
  >>
  /Subtype /Form
  /Type /XObject
>>
stream
/FRM Doendstream
endobj

18 0 obj
<<
  /BBox [ 41.3670998 713.710022 237.352997 781.257996 ]
  /FormType 1
  /Length 51
  /Matrix [ 1 0 0 1 0 0 ]
  /Resources <<
    /ProcSet [ /PDF /Text ]
    /XObject <<
      /n0 19 0 R
      /n2 11 0 R
    >>
  >>
  /Subtype /Form
  /Type /XObject
>>
stream
q 1 0 0 1 0 0 cm /n0 Do Q q 1 0 0 1 0 0 cm /n2 Do Qendstream
endobj

19 0 obj
<<
  /BBox [ 41.3670998 713.710022 237.352997 781.257996 ]
  /FormType 1
  /Length 9
  /Matrix [ 1 0 0 1 0 0 ]
  /Resources <<
    /ProcSet [ /PDF /Text ]
  >>
  /Subtype /Form
  /Type /XObject
>>
stream
% DSBlankendstream
endobj

xref
0 20
0000000000 65536 f 
0000000017 00000 n 
0000000123 00000 n 
0000000238 00000 n 
0000000320 00000 n 
0000000544 00000 n 
0000000634 00000 n 
0000000790 00000 n 
0000001026 00000 n 
0000001272 00000 n 
0000004664 00000 n 
0000004691 00000 n 
0000004990 00000 n 
0000005101 00000 n 
0000005196 00000 n 
0000006628 00000 n 
0000010882 00000 n 
0000022144 00000 n 
0000022423 00000 n 
0000022763 00000 n 

trailer
<<
  /Size 20
  /Info 6 0 R
  /Root 5 0 R
  /ID [ <D0151A7ADF2410CF3E72A04B9CB83452> <FA2030FCC5A66742887F23E51DEC5875> ]
>>

startxref
23003
%%EOF

It seems to be correct, since there's /n2 11 0 R in 18 0 obj and /Img0 16 0 R in 11 0 obj and 16 0 obj looks like a well-formed Image XObject.

But the image doesn't show up when I using pdf reader to open this pdf.

Could someone please give me a clue about what's wrong with this pdf file, and how to insert the image correctly.

Thanks a lot.

ps.
signed_with_image_only.pdf

signed_with_text_and_image.pdf

signed_with_image_only.pdf is the pdf I've added '\n' before 'endstream'. You can see image doesn't show up in the form which is below 'Hello World!'.

signed_with_text_and_image.pdf is pretty much the same as signed_with_image_only.pdf, but I put some text in layer n2 to show you text insertion is ok. The same image is also inserted, and it also not showed up.

answer 1

The transformation matrix is not right.

You can use other PDF utility, like PDFBox, to add an image into digital signature appearance, then analyze the transformation matrix on image painting, also pay attention on its BBOx.

answer 2

I don't know if it's the text formatting but you have a number of streams (objects 17, 18 and 19) where the "endstream" that denotes the end of the stream data is not being written correctly. The "endstream" should be on a line of its own, and a PDF file requires whitespace between tokens and operators. Therefore, things like

/FRM Doendstream

are invalid syntax, they should be written like

/FRM Do
endstream

For images, the current transform matrix must transform from the 1 unit square that images are specified in, to the desired size in PDF units. You're using a transformation matrix of [1 0 0 -1 0 1] to position and scale the image XObject, which will not give your desired result. Typically, for an unrotated image you'll want to use something like [width 0 0 height Xpos Ypos]. Also, I always recommend enclosing image XObject invocation in a graphic state save/restore so that the CTM required can be popped off the graphic state stack.

Finally, looking at the file in Dropbox your image is damaged. It specifies a width of 400 rasters and a height of 332 rasters - in RGB format, the image should have a length of 398,400 but only has a length of 11097. This is way too short for this image data stream. It looks like this is compressed data, in DCT compressed format. For this to render properly you need to specify that it is compressed and in the format, e.g. add

/Filter /DCTDecode

to the image dictionary.

------splitte line----------------------------
Related