Getting exception in with add signature in pdf file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Getting exception in with add signature in pdf file

kishore
This post has NOT been accepted by the mailing list yet.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.util.Date;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSignatureAppearance;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfSignatureAppearance.RenderingMode;
import com.itextpdf.text.pdf.security.BouncyCastleDigest;
import com.itextpdf.text.pdf.security.DigestAlgorithms;
import com.itextpdf.text.pdf.security.ExternalDigest;
import com.itextpdf.text.pdf.security.MakeSignature;
import com.itextpdf.text.pdf.security.MakeSignature.CryptoStandard;
import com.itextpdf.text.pdf.security.PrivateKeySignature;

public class C2_06_SignatureAppearance {

        public static final String KEYSTORE = "src/KeyStore.jks";
        public static final char[] PASSWORD = "PASSWORD".toCharArray();
        public static final String IMG = "src/canvas.png";
        public static final String SRC = "src/APP.pdf";
        public static final String DEST = "src/app1.html.pdf";
       

        public void sign(String src, String name, String dest,
                        Certificate[] chain, PrivateKey pk,
                        String digestAlgorithm, String provider,
                        CryptoStandard subfilter,
                        String reason, String location, RenderingMode renderingMode,
                        Image image)
                                        throws GeneralSecurityException, IOException, DocumentException {
        // Creating the reader and the stamper
        PdfReader reader = new PdfReader(src);
        FileOutputStream os = new FileOutputStream(dest);
        PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
        // Creating the appearance
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setReason(reason);
        appearance.setLocation(location);
        appearance.setVisibleSignature(name);
       
        appearance.setLayer2Text("Signed on " + new Date().toString());
        appearance.setRenderingMode(renderingMode);
        appearance.setSignatureGraphic(image);
        // Creating the signature
        PrivateKeySignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
        ExternalDigest digest = new BouncyCastleDigest();
        MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, subfilter);
        }
 
        public static void main(String[] args) throws GeneralSecurityException, IOException, DocumentException {
               
                BouncyCastleProvider provider = new BouncyCastleProvider();
               
                Security.addProvider(provider);
               
                KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
                ks.load(new FileInputStream(KEYSTORE), PASSWORD);
               
        String alias = (String)ks.aliases().nextElement();
        PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD);
       
        Certificate[] chain = ks.getCertificateChain(alias);
        Image image = Image.getInstance(IMG);
       
        C2_06_SignatureAppearance app = new C2_06_SignatureAppearance();
        app.sign(SRC, "Signature1", String.format(DEST, 1), chain, pk,
        DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS,
        "Appearance 1", "Ghent", RenderingMode.DESCRIPTION, null);
        app.sign(SRC, "Signature1", String.format(DEST, 2), chain, pk,
        DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS,
        "Appearance 2", "Ghent", RenderingMode.NAME_AND_DESCRIPTION, null);
        app.sign(SRC, "Signature1", String.format(DEST, 3), chain, pk,
        DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS,
        "Appearance 3", "Ghent", RenderingMode.GRAPHIC_AND_DESCRIPTION, image);
        app.sign(SRC, "Signature1", String.format(DEST, 4), chain, pk,
        DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS,
        "Appearance 4", "Ghent", RenderingMode.GRAPHIC, image);
        }
}

Exception in thread "main" java.lang.IllegalArgumentException: The field Signature1 does not exist.
        at com.itextpdf.text.pdf.PdfSignatureAppearance.setVisibleSignature(PdfSignatureAppearance.java:487)
        at C2_06_SignatureAppearance.sign(C2_06_SignatureAppearance.java:55)
        at C2_06_SignatureAppearance.main(C2_06_SignatureAppearance.java:81)