iText Java签名PDF DocumentException:空间不足 [英] iText Java Signing PDF DocumentException: Not enough space

查看:264
本文介绍了iText Java签名PDF DocumentException:空间不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText 5.5.5 for Java,我想创建具有外部签名的签名PDF,如下所示:

I am using iText 5.5.5 for Java and I would like to create signed PDF with external signature as follows:

获取应签署的PDF文档使用空签名创建PDF并提供BASE64编码的字节以由外部签名机制签名:

Take the PDF document that should be signed and create PDF with empty signature and provide BASE64 encoded bytes to be signed by external signature mechanism:

PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "test");
appearance.setCertificate(chain[1]);
ExternalSignatureContainer external = new ExternalBlankSignatureContainer(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
MakeSignature.signExternalContainer(appearance, external, 8192);

InputStream is = appearance.getRangeStream();
byte[] toSign = getBytes(is);
this.b64String = new String(Base64.encode(toSign));

使用提供签名的外部签名机制签署 b64String 因为PKCS#7在BASE64中签署了数据。

Sign b64String with external signature mechanism providing signature as PKCS#7 signed data in BASE64.

创建 ExternalSignatureContainer 以便从外部获得PKCS#7签名数据签名机制:

Create ExternalSignatureContainer to have just the PKCS#7 signed data from external signing mechanism:

public class MyExternalSignatureContainer implements ExternalSignatureContainer {
    protected byte[] sig;

    public MyExternalSignatureContainer(byte[] sig) {
        this.sig = sig;
    }

    @Override
    public void modifySigningDictionary(PdfDictionary arg0) {
    }

    @Override
    public byte[] sign(InputStream arg0) throws GeneralSecurityException {
        return sig;
    }
}

使用MyExternalSignatureContainer创建签名的PDF文档:

Create signed PDF document with MyExternalSignatureContainer:

PdfReader reader = new PdfReader(dest);
FileOutputStream os = new FileOutputStream(signedpdf);
ExternalSignatureContainer external = new MyExternalSignatureContainer(signedData);
MakeSignature.signDeferred(reader, "test", os, external);

但我上了最后一行 MakeSignature.signDeferred(读者,测试,os,external); 以下异常:

But I get on the last line MakeSignature.signDeferred(reader, "test", os, external); the following exception:

com.itextpdf.text.DocumentException: Not enough space

问题在哪里以及如何解决?

Where is the problem and how to resolve it?

推荐答案

您已经估计签名将适合8192个字节。但是,签名 byte [] 的字节数超过8192,因此异常空间不足。例如:您的外部签名容器返回一个测量10000字节的签名。 iText告诉你,10000大于8192并且你要求的东西是不可能的。

You have made an estimation that the signature will fit into 8192 bytes. However, the number of bytes of signature byte[] exceeds 8192, hence the exception Not enough space. For instance: your external signature container returns a signature that measures 10000 bytes. iText tells you that 10000 is bigger than 8192 and that you are asking something that is impossible.

如何解决这个问题:在创建PDF时做出更好的估计空签名。

How to fix this: make a better estimate when you create the PDF with the empty signature.

这篇关于iText Java签名PDF DocumentException:空间不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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