How to give alternate description for a field in Itext.?

Refresh

2 weeks ago

Views

4 time

0

I am trying to generate an accessible digitally signed PDF using iText.

I am able to create a pdf with digital signature but it is not accessible. I suppose it is because there is no alternate description for the field "sig".

How do i give the alternate description here?

public class C2_01_SignHelloWorld  {
public static final String KEYSTORE = "C:\\Users\\rajeshkumar_pani\\ks";
public static final char[] PASSWORD = "123456".toCharArray();
public static final String SRC = "C:\\Users\\rajeshkumar_pani\\Downloads\\letters (14)\\JohnPHD125.pdf";
public static final String DEST = "C:\\Users\\rajeshkumar_pani\\Downloads\\letters (14)\\hello_signed%s.pdf";

public void sign(String src, String dest,
                 Certificate[] chain,
                 PrivateKey pk, String digestAlgorithm, String provider,
                 PdfSigner.CryptoStandard subfilter,
                 String reason, String location)
        throws GeneralSecurityException, IOException {
    // Creating the reader and the signer
    PdfReader reader = new PdfReader(src);
    PdfSigner signer = new PdfSigner(reader, new FileOutputStream(dest), false);
    // Creating the appearance
    PdfSignatureAppearance appearance = signer.getSignatureAppearance()
            .setReason(reason)
            .setLocation(location)
            .setReuseAppearance(false);

    Rectangle rect = new Rectangle(350, 550, 200, 100);
    appearance
            .setPageRect(rect)
            .setPageNumber(1);
    signer.setFieldName("sig");   // NO ALTERNATE DESCRIPTION FOR "SIG"





    // Creating the signature
    IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
    IExternalDigest digest = new BouncyCastleDigest();
    signer.signDetached(digest, pks, chain, null, null, null, 0, subfilter);
}

public static void main(String[] args) throws GeneralSecurityException, IOException {
    BouncyCastleProvider provider = new BouncyCastleProvider();
    Security.addProvider(provider);
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(new FileInputStream(KEYSTORE), PASSWORD);
    String alias = 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(), PdfSigner.CryptoStandard.CMS, "Test 1", "BBSR");
    app.sign(SRC, String.format(DEST, 2), chain, pk, DigestAlgorithms.SHA512, provider.getName(), PdfSigner.CryptoStandard.CMS, "Test 2", "Ghent");
    app.sign(SRC, String.format(DEST, 3), chain, pk, DigestAlgorithms.SHA256, provider.getName(), PdfSigner.CryptoStandard.CADES, "Test 3", "Ghent");
    app.sign(SRC, String.format(DEST, 4), chain, pk, DigestAlgorithms.RIPEMD160, provider.getName(), PdfSigner.CryptoStandard.CADES, "Test 4", "Ghent");
}
}

0 answers