You need a valid certificate and can use this code:

  public static final boolean signPdf(String keystoreType,       // Keystore 
type. Can be KeyStore.getDefaultType()
                                      File keystore,             // The 
keystore file
                                      char[] keystorePassword,   // the 
password to the keystore
                                      char[] privateKeyPassword, // the 
passwort of the private key
                                      File pdfFile,              // the PDF 
input File
                                      String pdfOutfile,         // The whole 
filename of the PDF output File
                                      String reason,             // the reason 
for this signing
                                      String location,           // and the 
location where it was signed
                                      String userPassword,       // Username 
for encryption
                                      String ownerPassword       // Password 
used for encryption
                                      ) throws SignatureException, IOException, 
DocumentException {
    try {
      PrivateKey key = null;
      Certificate[] chain = null;
      if (!keystore.exists())
        return false;
      if (reason != null) {
        KeyStore ks = KeyStore.getInstance(keystoreType);
        FileInputStream fis = new FileInputStream(keystore);
        try {
          ks.load(fis, keystorePassword);
        }
        finally { fis.close(); }
        String alias = (String) ks.aliases().nextElement();
        key = (PrivateKey) ks.getKey(alias, privateKeyPassword);
        chain = ks.getCertificateChain(alias);
      }
      PdfReader pdfReader = new PdfReader(pdfFile.getAbsolutePath());
      File outputFile = new File(pdfOutfile);
      PdfStamper pdfStamper;
      if (reason != null) {
        pdfStamper = PdfStamper.createSignature(pdfReader, null, '\0', 
outputFile);
        PdfSignatureAppearance sap = pdfStamper.getSignatureAppearance();
        sap.setCrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED);
        sap.setReason(reason);
        sap.setLocation(location);
      }
      else {
        FileOutputStream fos = new FileOutputStream(outputFile);
        pdfStamper = new PdfStamper(pdfReader, fos);
      }
      if (userPassword != null)
        pdfStamper.setEncryption(true, userPassword, ownerPassword, 
PdfWriter.AllowPrinting | PdfWriter.AllowCopy | PdfWriter.AllowScreenReaders | 
PdfWriter.AllowDegradedPrinting);
      pdfStamper.setFormFlattening(true);
      pdfStamper.close();
      return true;
    }
    catch (KeyStoreException key) {
      throw new SignatureException(key);
    }
    catch (NoSuchAlgorithmException nsa) {
      throw new SignatureException(nsa);
    }
    catch (CertificateException ce) {
      throw new SignatureException(ce);
    }
    catch (UnrecoverableKeyException ur) {
      throw new SignatureException(ur);
    }
  }

This also sets Encryption and flags, but you can easily leave the userpassword 
to null to not encrypt the PDF.
If you want to add multiple signatures, you need to open the PDF in append mode:

        pdfStamper = PdfStamper.createSignature(pdfReader, null, '\0', 
outputFile, true);

And thats about it. Works with a valid PKCS12 certificate.

Regards,
Rog



It was Tue, 15 Aug 2006 16:35:54 +0800, a beautiful sunny day, until "Eric 
Chow" <[EMAIL PROTECTED]> wrote this:

> Hello,
> 
> Is there any simple PDF digital signature for reference?
> Sign(Multi-sign) a PDF and verify a PDF using iText?
> 
> Is it possible to add a signature stamp in a PDF ?
> 
> 
> Best regards,
> Eric
> 
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> iText-questions mailing list
> iText-questions@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/itext-questions


/*
 * Roger Misteli - ABACUS Research AG
 * The trouble with troubleshooting is that trouble sometimes shoots back!
 */



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to