如何在iText签名图章中添加自定义字段?

时间:2016-02-18 13:51:28

标签: java itext digital-signature

我有一个简单的签名方法来签署PDF文档正常工作,它使用显示名称,位置,日期和原因的默认戳记签署文档。

现在我的经理问我是否可以在其上再添加一个字段。我们假设它是一个"电子邮件"因为我还没有被告知他们想要什么。

我尝试搜索并应用我发现的一些东西,但没有任何效果。

此外,我被问到是否可以删除/隐藏这4个默认字段的标签(日期,原因等等),而我无法管理它并没有找到任何关于它的信息,我不知道它是否可以真正定制。

在这里,我将展示她的签名过程是如何运作的。在middel中,我在iText网站上找到了一些代码,并尝试用来添加"新字段"但没有显示任何与默认邮票不同的结果。

public String signPdfFirstTime(String src, String dest, PrivateKey pk, Certificate[] chain, String providerName, String conteudoBase64, X509Certificate cert) throws IOException, DocumentException, GeneralSecurityException {

    byte[] content = Base64.decode(conteudoBase64);

    FileOutputStream fos = new FileOutputStream(path + File.separator + src);
    fos.write(content );
    fos.close();

    File f = new File(path + File.separator + src);

    FileInputStream in = new FileInputStream(f);

    PdfReader reader = new PdfReader(in);

    FileOutputStream os = new FileOutputStream(path + File.separator + dest);
    PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
    // Creating the appearance
    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
    appearance.setReason("SOME REASON");
    appearance.setLocation("SOMEWHERE");
    //  appearance.getAppearance().curveFromTo(98, 120, 46, 94);
    Rectangle rectangle = new Rectangle(0, 0, 500, 70);

    appearance.setVisibleSignature(rectangle, 1, "SIGNATURE");

    //Here I trid to add fields to the stamp
    //NOT WORKING AS I EXPECTED...
    PdfWriter writer = stamper.getWriter();
    PdfFormField field = PdfFormField.createEmpty(writer);
    field.setFieldName("TEST 1111");
    TextField name = new TextField(writer,rectangle, "TEST 2222");
    PdfFormField personal_name = name.getTextField();
    field.addKid(personal_name);
    TextField password = new TextField(writer, rectangle, "password");
    PdfFormField personal_password = password.getTextField();
    field.addKid(personal_password);
    stamper.addAnnotation(field, 1);

    appearance.setCertificate(cert);
    // Creating the signature
    ExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, providerName);
    ExternalDigest digest = new BouncyCastleDigest();
    List<CrlClient> crlList = new ArrayList<CrlClient>();
    crlList.add(new CrlClientOnline());

    LtvVerification v = stamper.getLtvVerification();

    OcspClient ocspClient = new OcspClientBouncyCastle();

    String url = CertificateUtil.getCRLURL(cert);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    X509CRL crl = (X509CRL) cf.generateCRL(new URL(url).openStream());
    System.out.println("CRL valid until: " + crl.getNextUpdate());
    System.out.println("Certificate revoked: " + crl.isRevoked(chain[0]));

    if (crl.isRevoked(chain[0])) {

        throw new GeneralSecurityException("CERTIFICADO REVOGADO!");
    }
    else {
        MakeSignature.processCrl(cert, crlList);

        MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, CryptoStandard.CMS);
        os.close();
        byte[] b = this.read(f);
        return Base64.encodeBytes(b);
    }
}

1 个答案:

答案 0 :(得分:1)

PdfSignatureAppearance类允许您通过

设置自动生成的外观的文本
/**
 * Sets the signature text identifying the signer.
 * @param text the signature text identifying the signer. If <CODE>null</CODE> or not set
 * a standard description will be used
 */
public void setLayer2Text(String text)

使用的字体可以使用

设置
/**
 * Sets the n2 and n4 layer font. If the font size is zero, auto-fit will be used.
 * @param layer2Font the n2 and n4 font
 */
public void setLayer2Font(Font layer2Font)

如果您想进一步控制外观,可以检索第2层模板并设计所需的任何内容。

/**
 * Gets a template layer to create a signature appearance. The layers can go from 0 to 4,
 * but only layer 0 and 2 will be used if acro6Layers is true.
 * <p>
 * Consult <A HREF="http://partners.adobe.com/asn/developer/pdfs/tn/PPKAppearances.pdf">PPKAppearances.pdf</A>
 * for further details.
 * @param layer the layer
 * @return a template
 */
public PdfTemplate getLayer(int layer)