You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Wilfling Stefan <st...@softline.at> on 2011/11/03 18:33:00 UTC

Visible Signatures

Hi!

I´m new to pdfbox and I´m trying to add a visible Signature.
An invisible digital signature works fine, but to get an visible doesn´t work (I only get an rectangle on Mouse-Over).

Is there any simple way to add an visible Signature with the Default-Appearance (http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PPKAppearances.pdf) to a pdf-document?


I use the following code-part to create my

..

PDRectangle rect = rects.get(pageNo);

                    PDDocument documentSig = createPDocumentSignature(rect);
                    ByteArrayOutputStream outSig = new ByteArrayOutputStream();
                    documentSig.save(outSig);

                    SignatureOptions options = new SignatureOptions();
                    options.setPage(pageNo);
                    options.setVisualSignature(new ByteArrayInputStream(outSig.toByteArray()));

                    document.addSignature(pdsignature, signatureinterfaceimpl, options);

..


    private PDDocument createPDocumentSignature(PDRectangle rect) throws IOException {
        PDDocument documentSig = new PDDocument();
        PDPage pageSig = new PDPage();
        documentSig.addPage(pageSig);

        PDAnnotationText annot = new PDAnnotationText();
        annot.setRectangle(rect);
        pageSig.getAnnotations().add(annot);

        COSDictionary root = documentSig.getDocumentCatalog().getCOSDictionary();

        COSDictionary sig = new COSDictionary();
        root.setItem(COSName.SIG, sig);
        sig.setItem(COSName.FT, COSName.SIG);

        PDAppearanceDictionary ap = new PDAppearanceDictionary();
        sig.setItem(COSName.AP, ap.getDictionary());

        COSDictionary dr = new COSDictionary();
        dr.setItem(COSName.FONT, PDType1Font.HELVETICA);
        sig.setItem(COSName.DR, dr);

        return documentSig;
    }




-stefan

Re: AW: Visible Signatures

Posted by in...@rayman2200.de.
Am 04.11.2011 07:57, schrieb Wilfling Stefan:
> Hi!
>
> Thanks' for the fast answer.
>
>> The documentation for the visual signature could be better.
> Yes!
> The first problem was, that there is no description to create
> visible/invisible Signatures with pdfbox.

https://issues.apache.org/jira/browse/PDFBOX-912
At the bottom you will find a zip with an example code. Next time just 
ask :-)

>> The InputStream for the visualization expect a prepaired pdf with a
>> bunch of dictionaries. The pdfbox extract all needed dictionaries 
>> and
>> add it to the document. So you can't just add image bytes or so.
> I understand the system behind this (after some hours debugging ;-) 
> ).
> My problem is, that I didn´t want use two PDF-Libraries (iText + 
> PdfBox).

Yep, I don't want it too, but implementing the visual signature part, 
is too much work for me.

>> I think you asking yourself why the visual signature impl. was done 
>> so
>> complicated.
> At iText this
>
>> My problem was, I haven't enough functionality inside the pdfbox for
>> creating a table with an image and informations like name, location 
>> or
>> reason. So I decide using iText ;-) for creating the visualisation 
>> and
>> adding this as InputStream to the pdfbox. Not the best way, but the 
>> only
>> fast solution.
> Do you have an example?

Sry, no. The code for creating the visual signature is closed source. 
But I can try to extract a sample visual signature. So you just would 
need to pass the file as input stream to the VisualSignature method.

>> Hope you understand what I meaning. If not, I can try to explain it 
>> in
>> german. :-)
> I understand you and I think it´s better to stay at english (maybe
> our discussion helps other people).
>
>> If someone can implement something like tables in pdfbox, I can 
>> rewrite
>> the implementation and simplify it.
> I don´t think that a table is necessary for this.

Maybe not table, but a similar structure. The signature field should 
use a fixed size. So for example if the user will add an image and his 
name, the implementation need to resize the image and may be able to 
wrap the text in a new line, if necessary. So additionaly some font 
metric calculation is needed.

>
> Kind Regards
> Stefan

Best regards
Thomas


AW: Visible Signatures

Posted by Wilfling Stefan <st...@softline.at>.
Hi!

Thanks' for the fast answer.

> The documentation for the visual signature could be better.
Yes!
The first problem was, that there is no description to create visible/invisible Signatures with pdfbox.

> The InputStream for the visualization expect a prepaired pdf with a 
> bunch of dictionaries. The pdfbox extract all needed dictionaries and 
> add it to the document. So you can't just add image bytes or so.
I understand the system behind this (after some hours debugging ;-) ).
My problem is, that I didn´t want use two PDF-Libraries (iText + PdfBox).

> I think you asking yourself why the visual signature impl. was done so 
> complicated.
At iText this 

> My problem was, I haven't enough functionality inside the pdfbox for 
> creating a table with an image and informations like name, location or 
> reason. So I decide using iText ;-) for creating the visualisation and 
> adding this as InputStream to the pdfbox. Not the best way, but the only 
> fast solution.
Do you have an example?

> Hope you understand what I meaning. If not, I can try to explain it in 
> german. :-)
I understand you and I think it´s better to stay at english (maybe our discussion helps other people).

> If someone can implement something like tables in pdfbox, I can rewrite 
> the implementation and simplify it.
I don´t think that a table is necessary for this.


Kind Regards 
Stefan

Re: Visible Signatures

Posted by Thomas Chojecki <in...@rayman2200.de>.
Am 03.11.2011 18:33, schrieb Wilfling Stefan:
> Hi!
Hi Stefan,

> I´m new to pdfbox and I´m trying to add a visible Signature.
> An invisible digital signature works fine, but to get an visible doesn´t work (I only get an rectangle on Mouse-Over).
>
> Is there any simple way to add an visible Signature with the Default-Appearance (http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PPKAppearances.pdf) to a pdf-document?
>
>
> I use the following code-part to create my
>
> ..
>
> PDRectangle rect = rects.get(pageNo);
>
>                      PDDocument documentSig = createPDocumentSignature(rect);
>                      ByteArrayOutputStream outSig = new ByteArrayOutputStream();
>                      documentSig.save(outSig);
>
>                      SignatureOptions options = new SignatureOptions();
>                      options.setPage(pageNo);
>                      options.setVisualSignature(new ByteArrayInputStream(outSig.toByteArray()));
>
>                      document.addSignature(pdsignature, signatureinterfaceimpl, options);

The documentation for the visual signature could be better.

The InputStream for the visualization expect a prepaired pdf with a 
bunch of dictionaries. The pdfbox extract all needed dictionaries and 
add it to the document. So you can't just add image bytes or so.

I think you asking yourself why the visual signature impl. was done so 
complicated.

My problem was, I haven't enough functionality inside the pdfbox for 
creating a table with an image and informations like name, location or 
reason. So I decide using iText ;-) for creating the visualisation and 
adding this as InputStream to the pdfbox. Not the best way, but the only 
fast solution.

Hope you understand what I meaning. If not, I can try to explain it in 
german. :-)

If someone can implement something like tables in pdfbox, I can rewrite 
the implementation and simplify it.

Best regards
Thomas