Hello, i try tihs code:
public void SignDetached() {
try {
Security.addProvider(new BouncyCastleProvider());
//KeyStore ks = KeyStore.getInstance("PKCS12");
// ks.load(new FileInputStream("my_private_key.pfx"),
"password".toCharArray());
// String alias = (String)ks.aliases().nextElement();
// PrivateKey key = (PrivateKey)ks.getKey(alias,
"password".toCharArray());
// Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader("d:\\cert.pdf");
PdfStamper stp = PdfStamper.createSignature(reader, new
FileOutputStream("d:\\certFirm.pdf"), '\0');
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setVisibleSignature(new Rectangle(100, 100, 300, 200), 1, null);
sap.setSignDate(new GregorianCalendar());
// sap.setCrypto(null, chain, null, null);
sap.setReason("I like to sign");
sap.setLocation("Universe");
sap.setAcro6Layers(true);
sap.setRender(PdfSignatureAppearance.SignatureRenderNameAndDescription);
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE,
PdfName.ADBE_PKCS7_DETACHED);
dic.setDate(new PdfDate(sap.getSignDate()));
//
dic.setName(PdfPKCS7.getSubjectFields((X509Certificate)chain[0]).getField("CN"));
if (sap.getReason() != null)
dic.setReason(sap.getReason());
if (sap.getLocation() != null)
dic.setLocation(sap.getLocation());
sap.setCryptoDictionary(dic);
int csize = 4000;
HashMap exc = new HashMap();
exc.put(PdfName.CONTENTS, new Integer(csize * 2 + 2));
System.out.println("sap:"+sap+" ,exc: "+exc);
sap.preClose(exc);
CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
// generator.addSigner(key, (X509Certificate)chain[0],
CMSSignedDataGenerator.DIGEST_SHA1);
/* ArrayList list = new ArrayList();
for (int i = 0; i < chain.length; i++) {
list.add(chain[i]);
}*/
// CertStore chainStore = CertStore.getInstance("Collection", new
CollectionCertStoreParameters(list), "BC");
// generator.addCertificatesAndCRLs(chainStore);
CMSProcessable content = new CMSProcessableRange(sap);
CMSSignedData signedData = generator.generate(content, false, "BC");
//byte[] pk = signedData.getEncoded();
//baos.toByteArray() contents the pk7 file.
byte[] pk = baos.toByteArray();
csize=pk.length;
byte[] outc = new byte[csize];
PdfDictionary dic2 = new PdfDictionary();
System.arraycopy(pk, 0, outc, 0, pk.length);
dic2.put(PdfName.CONTENTS, new PdfString(outc).setHexWriting(true));
sap.close(dic2);
}
catch (Exception e) {
e.printStackTrace();
}
}
and recived :
java.lang.NullPointerException
at com.lowagie.text.pdf.PdfSignatureAppearance.getAppearance(Unknown
Source)
at com.lowagie.text.pdf.PdfSignatureAppearance.preClose(Unknown Source)
at firma.PruebaFirma.SignDetached(PruebaFirma.java:80)
at firma.PruebaFirma.main(PruebaFirma.java:199)
i don´t know what is the reason...
Thanks, Ángel.
---------- Forwarded message ----------
From: amcereijo cereijo <[email protected]>
Date: 2009/4/22
Subject: Re: Add sign to pdf
To: [email protected]
Thanks i'm going to try it
>
> Message: 5
> Date: Wed, 22 Apr 2009 15:28:31 +0100
> From: Paulo Soares <[email protected]>
> Subject: Re: [iText-questions] Add sign to pdf
> To: Post all your questions about iText here
> <[email protected]>
> Message-ID:
> <[email protected]>
> Content-Type: text/plain; charset="utf-8"
>
> See http://itextpdf.sourceforge.net/howtosign.html#signextdiccms. Replace
> signedData.getEncoded() with your pkcs7.
>
> Paulo
>
> > -----Original Message-----
> > From: amcereijo cereijo [mailto:[email protected]]
> > Sent: Wednesday, April 22, 2009 3:21 PM
> > To: [email protected]
> > Subject: Re: [iText-questions] Add sign to pdf
> >
> >
> >
> > First, thanks to all.
> > Second, i have seen that in data base is stored the pk7 file
> > thath containst a certificate.
> > Well, i have seen some examples of itext but i don?t know how
> > to add this digital sign or to sign a pdf file whith this pk7 file.
> >
> > Thanks again, ?ngel.
> >
> >
> >
> >
> >
> >
> > Message: 6
> > Date: Tue, 21 Apr 2009 13:52:57 -0700
> > From: Leonard Rosenthol <[email protected]>
> > Subject: Re: [iText-questions] Add sign to pdf
> > To: Post all your questions about iText here
> > <[email protected]>
> > Message-ID:
> > <c613fec9.4ee97%[email protected]<c613fec9.4ee97%[email protected]>
> > <mailto:c613fec9.4ee97%[email protected]<c613fec9.4ee97%[email protected]>>
> >
>
> > Content-Type: text/plain; charset="iso-8859-1"
> >
> > A signature includes a certificate - that's how the
> > technology works. If you don't have the certificate - you
> > can't sign ANYTHING. (not a PDF issue, but a Digital
> > Signature issue).
> >
> > Leonard
> >
> >
> > On 4/21/09 5:38 PM, "amcereijo cereijo"
> > <[email protected]> wrote:
> >
> > Sorry, my english is very bad.
> > I want to know if it?s possible add a sign to a pdf file.
> > I?m not access to certificate, i only have the content
> > of the sign (bytes[]) and i want to add this content to pdf file.
> >
> > I hope that you can understant me.
> >
> > Thanks, ?ngel.
> >
> >
> >
> > Message: 2
> > Date: Tue, 21 Apr 2009 15:32:42 +0200
> > From: 1T3XT info <[email protected]>
> > Subject: Re: [iText-questions] Add sign to pdf
> > To: Post all your questions about iText here
> > <[email protected]>
> > Message-ID: <[email protected]>
> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> >
> > amcereijo cereijo wrote:
> > > Hi, now is possible to add a sign saved in a data
> > base to pdf file?. The
> > > sign saved is the result to signed the pdf file in a
> > external plataform.
> >
> > I don't understand the question.
> >
> > Are you talking about digital signatures?
> > Or just about a "sign", some "image"?
> > You'll have to clarify.
> >
> > Try explaining what you want to do in pseudo code.
> > Maybe that will be easier for us to understand.
> > --
> > This answer is provided by 1T3XT BVBA
> > http://www.1t3xt.com/ - http://www.1t3xt.info
> >
> >
> >
> >
> >
> >
> > --
> > Leonard Rosenthol
> > PDF Standards Architect
> > Adobe Systems Incorporated
> > -------------- next part --------------
> > An HTML attachment was scrubbed...
> >
> > ------------------------------
> >
> > Message: 7
> > Date: Tue, 21 Apr 2009 14:28:32 -0700 (PDT)
> > From: mkl <[email protected]>
> > Subject: Re: [iText-questions] Add sign to pdf
> > To: [email protected]
> > Message-ID: <[email protected]>
> > Content-Type: text/plain; charset=UTF-8
> >
> >
> > Maybe you should post some code representing a short
> > form of what you
> > actually currently do as was already suggested before.
> >
> > Your sentence "The sign saved is the result to signed
> > the pdf file in a
> > external plataform" from your first message might be
> > interpreted in the way
> > that you have an external signature for the PDF and
> > somehow want to make it
> > an integrated one. If this is the case, you
> > unfortunately are out of luck as
> > integrated signatures require the PDF document to be
> > prepared in a certain
> > way and the digest value to be calculated from certain
> > parts of this
> > prepared document.
> >
> >
> > amcereijo cereijo wrote:
> > >
> > > Sorry, my english is very bad.
> > > I want to know if it?s possible add a sign to a pdf file.
> > > I?m not access to certificate, i only have the
> > content of the sign
> > > (bytes[]) and i want to add this content to pdf file.
> > >
> > > I hope that you can understant me.
> > >
> > > Thanks, ?ngel.
> > >
> > >
> > >> Message: 2
> > >> Date: Tue, 21 Apr 2009 15:32:42 +0200
> > >> From: 1T3XT info <[email protected]>
> > >> Subject: Re: [iText-questions] Add sign to pdf
> > >> To: Post all your questions about iText here
> > >> <[email protected]>
> > >> Message-ID: <[email protected]>
> > >> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> > >>
> > >> amcereijo cereijo wrote:
> > >> > Hi, now is possible to add a sign saved in a data
> > base to pdf file?.
> > >> The
> > >> > sign saved is the result to signed the pdf file in
> > a external
> > >> plataform.
> > >>
> > >> I don't understand the question.
> > >>
> > >> Are you talking about digital signatures?
> > >> Or just about a "sign", some "image"?
> > >> You'll have to clarify.
> > >>
> > >> Try explaining what you want to do in pseudo code.
> > >> Maybe that will be easier for us to understand.
>
>
> Aviso Legal:
> Esta mensagem ? destinada exclusivamente ao destinat?rio. Pode conter
> informa??o confidencial ou legalmente protegida. A incorrecta transmiss?o
> desta mensagem n?o significa a perca de confidencialidade. Se esta mensagem
> for recebida por engano, por favor envie-a de volta para o remetente e
> apague-a do seu sistema de imediato. ? proibido a qualquer pessoa que n?o o
> destinat?rio de usar, revelar ou distribuir qualquer parte desta mensagem.
>
> Disclaimer:
> This message is destined exclusively to the intended receiver. It may
> contain confidential or legally protected information. The incorrect
> transmission of this message does not mean the loss of its confidentiality.
> If this message is received by mistake, please send it back to the sender
> and delete it from your system immediately. It is forbidden to any person
> who is not the intended receiver to use, distribute or copy any part of this
> message.
>
>
> ------------------------------
>
>
> ------------------------------------------------------------------------------
> Stay on top of everything new and different, both inside and
> around Java (TM) technology - register by April 22, and save
> $200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
> 300 plus technical and hands-on sessions. Register today.
> Use priority code J9JMT32. http://p.sf.net/sfu/p
>
> ------------------------------
>
> _______________________________________________
> iText-questions mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
>
> End of iText-questions Digest, Vol 35, Issue 88
> ***********************************************
>
------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php