1

I'm working on another code for signing pdf with iText. Libraries are new and downloaded form source of the code. I'm new to java and iText. Sorry for the long post in code:

package c2_01_signhelloworld;

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 org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSignatureAppearance;
import com.itextpdf.text.pdf.PdfStamper;
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.ExternalSignature;
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_01_SignHelloWorld {

    public static final String KEYSTORE = "C:/Users/Documents/itext/2/ks";
    public static final char[] PASSWORD = "password".toCharArray();
    public static final String SRC = "C:/Users/Documents/itext/2/unsigned1.pdf";
    public static final String DEST = "C:/Users/Documents/itext/2/unsigned1_signed.pdf";

    public void sign(String src, String dest,
            Certificate[] chain,
            PrivateKey pk, String digestAlgorithm, String provider,
            CryptoStandard subfilter,
            String reason, String location)
                    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(new Rectangle(36, 748, 144, 780), 1, "sig");
        // Creating the signature
        ExternalDigest digest = new BouncyCastleDigest();
        ExternalSignature signature = new PrivateKeySignature(pk, digestAlgorithm, provider);
        MakeSignature.signDetached(appearance, digest, signature, 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);
        C2_01_SignHelloWorld app = new C2_01_SignHelloWorld();
        app.sign(SRC, String.format(DEST, 1), chain, pk, DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS, "Test 1", "Ghent");
        app.sign(SRC, String.format(DEST, 2), chain, pk, DigestAlgorithms.SHA512, provider.getName(), CryptoStandard.CMS, "Test 2", "Ghent");
        app.sign(SRC, String.format(DEST, 3), chain, pk, DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CADES, "Test 3", "Ghent");
        app.sign(SRC, String.format(DEST, 4), chain, pk, DigestAlgorithms.RIPEMD160, provider.getName(), CryptoStandard.CADES, "Test 4", "Ghent");
    }
}

After running code i get this error:

Exception in thread "main" java.lang.NoSuchMethodError: com.itextpdf.text.pdf.PdfSignatureAppearance.setCertificate(Ljava/security/cert/Certificate;)V
    at com.itextpdf.text.pdf.security.MakeSignature.signDetached(MakeSignature.java:115)
    at c2_01_signhelloworld.C2_01_SignHelloWorld.sign(C2_01_SignHelloWorld.java:59)
    at c2_01_signhelloworld.C2_01_SignHelloWorld.main(C2_01_SignHelloWorld.java:71)
Java Result: 1

This code makes these two files,but when i try to open unsigned1_signed.pdf it gets error..

3
  • 1
    It is most likely version problem. Check your jars. Commented Jan 24, 2013 at 9:01
  • 1
    As @madhead said, check your jars; it looks like you have different versions of iText classes on your classpath. The method missing in your context and its caller have been added last June during a major signature related code reorganization. Commented Jan 24, 2013 at 9:19
  • Yes, i just used iText 5.3.5 resources and it works well. Thank you Commented Jan 24, 2013 at 9:49

2 Answers 2

2

It means that libs you are using in project are not the same as system libs.

And so does not have same methods.

Sign up to request clarification or add additional context in comments.

Comments

0

Using just iText 5.3.5 fixes this problem. Older versions of iText libraries don't work with this code. You can check itext 5.4.0 snapshot for PdfSignatureAppearance here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.