当我使用带有电子令牌的IText签署PDF时,签署的pdf显示“至少有一个签名无效”#39;在Acrobat Adobe Reader中。我使用的是有效的电子令牌。以下是签署Pdf的代码。
//path of destination file String destFile = "D://sign_test.pdf"; // load key store KeyStore ks = KeyStore.getInstance("Windows-MY"); ks.load(null, "password".toCharArray()); Enumeration enumeration = ks.aliases(); String alias = null; while(enumeration.hasMoreElements()){ alias = (String)enumeration.nextElement(); if(alias.equalsIgnoreCase("alias of the certificate"))// to get specific certificate from keystore break; } PrivateKey pk = (PrivateKey)ks.getKey(alias, "password".toCharArray()); Certificate[] chain = ks.getCertificateChain(alias); PdfReader reader = new PdfReader(sourceFile); FileOutputStream os = new FileOutputStream(destFile); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); PdfSignatureAppearance appearance = stamper .getSignatureAppearance(); appearance.setReason("I've written this."); appearance.setLocation("Foobar"); appearance.setVisibleSignature(new Rectangle(72, 732, 144, 780),1,"first"); ExternalSignature es = new PrivateKeySignature(pk, "SHA-256", "SunMSCAPI"); ExternalDigest digest = new ProviderDigest("SunMSCAPI"); MakeSignature.signDetached(appearance, digest, es, chain, null, null, null, 0, CryptoStandard.CMS); stamper.close();