Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

exception - iText Java Signing PDF DocumentException: Not enough space

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

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

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

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...