如何在iText签名图章中添加自定义字段? [英] How to add custom field in iText signature stamp?

查看:73
本文介绍了如何在iText签名图章中添加自定义字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的签名方法来对工作正常的PDF文档进行签名,它使用默认的图章对文档进行签名,该图章显示名称,位置,日期和原因.

I have a simple signature method to sign PDF documents working fine, it signs the documents with the default stamp that shows Name, Location, Date and Reason.

现在,我的经理问我是否可以再添加一个字段.让我们假设这是一个电子邮件"字段,因为我没有确切告诉他们他们想要什么.

Now my manager asked me if it is possible to add one more field on it. Let's suppose it is an "Email" field as I haven't been told exactly what they want.

我尝试搜索并应用发现的内容,但没有任何效果.

I tried searching and apply somethings I found but nothing has worked.

另外,有人问我是否可以删除/隐藏这4个默认字段的标签(日期,原因等),我也无法做到这一点,但也找不到任何相关信息,我没有不知道它是否可以真正定制.

Also, I was asked if it is possible to remove/hide the label of those 4 default fields (Date, Reason, etc...) and I couldn't manage doing it neither find anything about it, I don't know if it can be really customized.

在这里,我将展示她的签名流程如何工作.在middel中,我在iText网站上找到了一些代码,并尝试使用它们添加新字段",但结果与默认戳记没有任何不同.

Here I will show how she signature process are working. In the middel there is some code I found on iText site and tried to use to add the "new field" but without any result different from the default stamp been showed.

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);
    }
}

推荐答案

PdfSignatureAppearance类允许您设置自动生成的外观的文本,

The PdfSignatureAppearance class allows you to set the text of the automatically generated appearance by

/**
 * 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层模板并在其上设计任何内容.

If you want to control the appearance even more, you can retrieve the layer 2 template and design any content on it you want.

/**
 * 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)

这篇关于如何在iText签名图章中添加自定义字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆