SELF_SIGNED cannot be resolved or is not a field

2019-06-03 18:02发布

问题:

When I tried to create an android application that sign a pdf file using itext, I get this error:

SELF_SIGNED cannot be resolved or is not a field

The error is in the line 32

sap.setCrypto(key, (java.security.cert.Certificate[]) chain, null, PdfSignatureAppearance.SELF_SIGNED);

Here there is the code:

public class Sign_pdf {

static String fname  = "C:\\Ressource\\test.pdf" ;
static String fnameS = "C:\\Ressource\\test1_sign.pdf" ;

public static final boolean signPdf()
        throws IOException, DocumentException, Exception
{
    // Vous devez preciser ici le chemin d'acces a votre clef pkcs12
    String fileKey          = "C:\\Ressource\\Nacer_Heithem.p12" ;
    // et ici sa "passPhrase"
    String fileKeyPassword  = "123456" ;

    try {
        // Creation d'un KeyStore
        KeyStore ks = KeyStore.getInstance("pkcs12");
        // Chargement du certificat p12 dans el magasin
        ks.load(new FileInputStream(fileKey), fileKeyPassword.toCharArray());
        String alias = (String)ks.aliases().nextElement();
        // Recupération de la clef privée
        PrivateKey key = (PrivateKey)ks.getKey(alias, fileKeyPassword.toCharArray());
        // et de la chaine de certificats
        Certificate[] chain = (Certificate[]) ks.getCertificateChain(alias);

        // Lecture du document source
        PdfReader pdfReader = new PdfReader((new File(fname)).getAbsolutePath());
        File outputFile = new File(fnameS);
        // Creation du tampon de signature
        PdfStamper pdfStamper;
        pdfStamper = PdfStamper.createSignature(pdfReader, null, '\0', outputFile);
        PdfSignatureAppearance sap = pdfStamper.getSignatureAppearance();
        sap.setCrypto(key, (java.security.cert.Certificate[]) chain, null, PdfSignatureAppearance.SELF_SIGNED);
        sap.setReason("Test SignPDF berthou.mc");
        sap.setLocation("");
        // Position du tampon sur la page (ici en bas a gauche page 1)
        sap.setVisibleSignature(new Rectangle(10, 10, 50, 30), 1, "sign_rbl");

        pdfStamper.setFormFlattening(true);
        pdfStamper.close();

        return true;
    }
    catch (Exception key) {
        throw new Exception(key);
    }
}