使用itext7无法在Digital Sign中获得自定义外观

问题描述

感谢任何帮助,因为我正在使用带有弹性城堡1.66(在数字签名期间自定义文本外观)的itext7 java。尽管我使用的是来自itext7的示例,但以某种方式,它既没有反映出图像,也没有反映出文本张贴标志。提前致谢。 最后确定并粘贴了完整的工作代码以使社区受益。

import java.io.File;
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 java.time.Instant;
import java.time.ZoneId;
import java.time.zoneddatetime;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.StampingProperties;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.signatures.BouncyCastleDigest;
import com.itextpdf.signatures.DigestAlgorithms;
import com.itextpdf.signatures.IExternalDigest;
import com.itextpdf.signatures.IExternalSignature;
import com.itextpdf.signatures.PdfSignatureAppearance;
import com.itextpdf.signatures.PdfSigner;
import com.itextpdf.signatures.PrivateKeySignature;

public class C2_05_CustomAppearance_V2 {

    public static final String DEST = "itextpdf_out/";

    public static final String KEYSTORE = "signature/some.pfx";
    public static final String SRC = "itextpdf_out/hello.pdf";

    public static final char[] PASSWORD = "1234".tochararray();

    public static final String[] RESULT_FILES = new String[] { "hello_sign_custom.pdf" };

    public static final String CR_LF = "\r\n";

    public static final String VISIBLE_SIGN_TEXT = "For XYZ Private Ltd" + CR_LF
            + "Digitally signed by Author" + CR_LF + "Authorised Signatory";

    static final float RECT_x = 350f;
    static final float RECT_y = 410f;
    static final float RECT_width = 130f;
    static final float RECT_height = 40f;

    // public static final String VISIBLE_SIGN_TEXT = "This document was signed
    // by Bruno Specimen.";

    public void sign(String src,String name,String dest,Certificate[] chain,PrivateKey pk,String digestAlgorithm,String provider,PdfSigner.CryptoStandard subfilter,String reason,String location,String visibleSignText,String tzId) throws GeneralSecurityException,IOException {

        PdfReader reader = new PdfReader(src);
        PdfSigner signer = new PdfSigner(reader,new FileOutputStream(dest),new StampingProperties());
        PdfDocument pdfDocument = signer.getDocument();
        /**
         * Get Zone DateTime
         */
        zoneddatetime zdt = Instant.Now().atZone(ZoneId.of(tzId));
        
        // Create the signature appearance
        PdfSignatureAppearance appearance = signer.getSignatureAppearance().setReason(reason).setLocation(location);
        appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION);
        /**
         * ID: 20200910 - This is must for signature to appear visible
         */
        appearance.setPageRect(new Rectangle(RECT_x,RECT_y,RECT_width,RECT_height));
        // This name corresponds to the name of the field that already exists in
        // the document.
        signer.setFieldName(name);

        // Get the background layer and draw a gray rectangle as a background.
        PdfFormXObject n0 = appearance.getLayer0();

        float x = n0.getBBox().toRectangle().getLeft();
        float y = n0.getBBox().toRectangle().getBottom();
        float width = n0.getBBox().toRectangle().getWidth();
        float height = n0.getBBox().toRectangle().getHeight();

        PdfCanvas canvas_n0 = new PdfCanvas(n0,pdfDocument);
        canvas_n0.setFillColor(ColorConstants.WHITE);
        canvas_n0.rectangle(x,y,width,height);
        canvas_n0.fill();

        // Set the signature information on layer 2
        PdfFormXObject n2 = appearance.getLayer2();
        Paragraph p = new Paragraph(visibleSignText + CR_LF + "Date: " + zdt);

        p.setHeight(RECT_height);
        p.setWidth(RECT_width);
        p.setFontColor(ColorConstants.BLACK);
        p.setFontSize(5.0f);

        Canvas canvas_n2 = new Canvas(n2,pdfDocument);

        // ImageData imgData = ImageDataFactory.create("images/scissor.png");
        // Image image = new Image(imgData);
        // canvas_n2.add(image);
        
        canvas_n2.add(p);
        canvas_n2.close();

        IExternalSignature pks = new PrivateKeySignature(pk,digestAlgorithm,provider);
        IExternalDigest digest = new BouncyCastleDigest();

        // Sign the document using the detached mode,CMS or CAdES equivalent.
        signer.signDetached(digest,pks,chain,null,subfilter);

        pdfDocument.close();
    }

    /**
     * Driver
     * 
     * @param args
     * @throws IOException
     * @throws GeneralSecurityException
     */
    public static void main(String[] args) throws IOException,GeneralSecurityException {
        System.out.println("Processing...");
        File file = new File(DEST);
        file.mkdirs();

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

        String reason = "Custom Appearance Example";
        String location = "INDIA";
        String tzId = "Asia/Calcutta";

        new C2_05_CustomAppearance_V2().sign(SRC,"sig",DEST + RESULT_FILES[0],pk,DigestAlgorithms.SHA256,provider.getName(),PdfSigner.CryptoStandard.CMS,reason,location,VISIBLE_SIGN_TEXT,tzId);
        System.out.println("Done!!!");
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)