Hi Randolph,
i don't think, you can use the PdfCopy-class as you need to add an AcroForm
to your Output-PDF. I use PdfWriter for this.

Have a look at the code attached to produce the template for the Output-PDF.
The signature has to be an indirect object.
Therefore I added a method to PdfFormField:
        public void setValueIndirect(PdfSignature aPdfSignature) throws IOException
        {
                PdfIndirectObject aIndirectSig = writer.addToBody(aPdfSignature);
                PdfIndirectReference aIndirectSigReference =
aIndirectSig.getIndirectReference();
                put(PdfName.V, aIndirectSigReference);
        }

Hopefully the other unresolved methods in the code below are 
self-explanatory.

Best regards,
Peter

P.S.: a hidden signature is invisible in the PDF-Text, but in the special
"signature"-tab 
of Acrobat Reader left of the PDF-text you can look at all the signature
details.


Here's the code:

        protected byte [] writeTemplatePDF(byte [] byteInPDF,  
                                                        String strSigFieldName, float 
llx, float lly, float urx, float ury,
                                                        PdfSignature2 aPdfSignature) 
{               ByteArrayOutputStream aStream = new ByteArrayOutputStream();
                Document document = null;
                PdfWriter  aWriter = null;
                
                PdfReader reader = new PdfReader(byteInPDF);
                char chPdfVersion = reader.getPdfVersion();

                if (reader.isEncrypted())
                {
                        throw new DocumentException("Input-PDF for digitally signing 
may not be
encrypted.");
                }

                int n = reader.getNumberOfPages();

                PRAcroForm aAcroForm = reader.getAcroForm();
                if (null != aAcroForm)
                {
                        throw new DocumentException("Input-PDF for digitally signing 
may not
contain an AcroForm.");
                }
                
                document = new Document(reader.getPageSizeWithRotation(1));
                aWriter = PdfWriter.getInstance(document, aStream);

                // the PDF Version of the input PDF is set here, too
                aWriter.setPdfVersion(chPdfVersion);

                if (mfEncryptionActive)
                {
                        aWriter.setEncryption(mBytesUserPassword, mBytesOwnerPassword,
                                                                        mPermissions, 
mfStrength128Bits);
                }
                document.open();

                PdfContentByte cb = aWriter.getDirectContent();

                PdfAcroForm acroForm = aWriter.getAcroForm();
                
                // copy all pages
                PdfImportedPage page;
                for (int i = 1; i <= n; i++ ) 
                {
                        document.newPage();
                        page = aWriter.getImportedPage(reader, i);
                        if (1 == i)
                        {
                                if (null != acroForm)
                                {
                                        if (isHidden())
                                        {
                                                // The signature should not be shown. 
We don't need all that layer
stuff
                                                PdfFormField aSignatureField = 
PdfFormField.createSignature(aWriter);
                                                
acroForm.setSignatureParams(aSignatureField, name, llx, lly, urx,
ury);

                                                
acroForm.drawSignatureAppearencesHidden(aSignatureField, llx, lly,
urx, ury);              
                                                                                       
         
                                                acroForm.addFormField(aSignatureField);

                                                
aSignatureField.setValueIndirect((PdfSignature)aPdfSignature);
                                                
acroForm.setSigFlags(PdfWriter.SIGNATURE_APPEND_ONLY |
PdfWriter.SIGNATURE_EXISTS);
                                        }
                                        else
                                        {
                                                throw new DocumentException("Visible 
Signatures are not yet
implemented.");
                                        }
                                }
                                
                        }
                        cb.addTemplate(page, 0f, 0f);
                }


                document.close();
                aStream.close();
                
                return aStream.toByteArray();
}


-----Urspr�ngliche Nachricht-----
Von: DI Randolph Kepplinger [mailto:[EMAIL PROTECTED] 
Gesendet: Dienstag, 15. Juni 2004 12:20
An: Peter Soelter
Cc: [EMAIL PROTECTED]
Betreff: Re: [iText-questions] signature problem with attached document


hello!

> You can use the PdfAcroForm for this.
>
>     PdfAcroForm acroForm = aWriter.getAcroForm();
>
> and
>
>     PdfFormField.createSignature(PdfWriter writer);

i don't understand this.
what i did is

                // build the signature
                PdfSignature pdfs=new PdfSignature("Adobe.PPKLite");
        ...
                pdfs.setString("ByteRange",new String(brange));
                pdfs.setSubFilter("adbe.pkcs7.detached");
                sig=bytesToHex(sig).getBytes();
                pdfs.setContents(sig);

                pdfs.setName("10.10.10.200");
                pdfs.put(PdfName.TYPE, new PdfName("Sig"));
        ...
                // read and copy the object
          reader=new PdfReader(INFILE);
                n = reader.getNumberOfPages();
                document = new Document(reader.getPageSizeWithRotation(1));
                baos=new ByteArrayOutputStream();
                writer = new PdfCopy(document, new
FileOutputStream("outfileunsigned.pdf"));
                document.open();
                    for (int i = 0; i < n; ) {
                        ++i;
                        page = writer.getImportedPage(reader, i);
                        writer.addPage(page);
                        System.out.println("Processed page " + i);
                    }
                    form = reader.getAcroForm();
                    if (form != null)
                    {
                        System.err.println("COPYING FORM!!!!");
                        writer.copyAcroForm(reader);
                    }
                    // here i add the signature
                    writer.addToBody( pdfs );
                    document.close();

        // after this i manipulate the byte-array

how can i use
                    PdfAcroForm acroForm = writer.getAcroForm();
                    PdfFormField.createSignature(writer);
instead or additional?
i have build a signature object, is this useless for adding a formfield?

> With the standard implementation of iText you can generate
> a hidden signature (x,y,dx,dy all 0).
what is a hidden signature, does this mean it never shows in acrobat? what
is this good for?

> But to create a visible signature you'll have to do a lot more. I 
> didn't manage to do it (luckily it wasn't a requirement in my case).
i dont need to see the signature like a watermark, but when i go to the
acrobat menu signatures, i'd like to see it.

regards

randolph




-- 
+++ Jetzt WLAN-Router f�r alle DSL-Einsteiger und Wechsler +++
GMX DSL-Powertarife zudem 3 Monate gratis* http://www.gmx.net/dsl



-------------------------------------------------------
This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to