Search in sources :

Example 21 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class TrueTypeFont method readStandardString.

/**
 * Reads a <CODE>String</CODE> from the font file as bytes using the Cp1252
 *  encoding.
 * @param length the length of bytes to read
 * @return the <CODE>String</CODE> read
 * @throws IOException the font file could not be read
 */
protected String readStandardString(int length) throws IOException {
    byte[] buf = new byte[length];
    rf.readFully(buf);
    try {
        return new String(buf, WINANSI);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) DocumentException(pdftk.com.lowagie.text.DocumentException)

Example 22 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class TrueTypeFontSubSet method readStandardString.

/**
 * Reads a <CODE>String</CODE> from the font file as bytes using the Cp1252
 *  encoding.
 * @param length the length of bytes to read
 * @return the <CODE>String</CODE> read
 * @throws IOException the font file could not be read
 */
protected String readStandardString(int length) throws IOException {
    byte[] buf = new byte[length];
    rf.readFully(buf);
    try {
        return new String(buf, BaseFont.WINANSI);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) DocumentException(pdftk.com.lowagie.text.DocumentException)

Example 23 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class PdfSigGenericPKCS method setSignInfo.

/**
 * Sets the crypto information to sign.
 * @param privKey the private key
 * @param certChain the certificate chain
 * @param crlList the certificate revocation list. It can be <CODE>null</CODE>
 */
public void setSignInfo(PrivateKey privKey, Certificate[] certChain, CRL[] crlList) {
    try {
        pkcs = new PdfPKCS7(privKey, certChain, crlList, hashAlgorithm, provider, PdfName.ADBE_PKCS7_SHA1.equals(get(PdfName.SUBFILTER)));
        pkcs.setExternalDigest(externalDigest, externalRSAdata, digestEncryptionAlgorithm);
        if (PdfName.ADBE_X509_RSA_SHA1.equals(get(PdfName.SUBFILTER))) {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            for (int k = 0; k < certChain.length; ++k) {
                bout.write(certChain[k].getEncoded());
            }
            bout.close();
            setCert(bout.toByteArray());
            setContents(pkcs.getEncodedPKCS1());
        } else
            setContents(pkcs.getEncodedPKCS7());
        name = PdfPKCS7.getSubjectFields(pkcs.getSigningCertificate()).getField("CN");
        if (name != null)
            put(PdfName.NAME, new PdfString(name, PdfObject.TEXT_UNICODE));
        pkcs = new PdfPKCS7(privKey, certChain, crlList, hashAlgorithm, provider, PdfName.ADBE_PKCS7_SHA1.equals(get(PdfName.SUBFILTER)));
        pkcs.setExternalDigest(externalDigest, externalRSAdata, digestEncryptionAlgorithm);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 24 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class PdfStamper method close.

/* ssteward omit: 
    private String getNewSigName() {
        AcroFields af = getAcroFields();
        String name = "Signature";
        int step = 0;
        boolean found = false;
        while (!found) {
            ++step;
            String n1 = name + step;
            if (af.getFieldItem(n1) != null)
                continue;
            n1 += ".";
            found = true;
            for (Iterator it = af.getFields().keySet().iterator(); it.hasNext();) {
                String fn = (String)it.next();
                if (fn.startsWith(n1)) {
                    found = false;
                    break;
                }
            }
        }
        name += step;
        return name;
    }
    */
/**
 * Closes the document. No more content can be written after the
 * document is closed.
 * <p>
 * If closing a signed document with an external signature the closing must be done
 * in the <CODE>PdfSignatureAppearance</CODE> instance.
 * @throws DocumentException on error
 * @throws IOException on error
 */
public void close() throws DocumentException, IOException {
    if (!hasSignature) {
        stamper.close(moreInfo);
        return;
    }
    sigApp.preClose();
    PdfSigGenericPKCS sig = sigApp.getSigStandard();
    PdfLiteral lit = (PdfLiteral) sig.get(PdfName.CONTENTS);
    int totalBuf = (lit.getPosLength() - 2) / 2;
    byte[] buf = new byte[8192];
    int n;
    InputStream inp = sigApp.getRangeStream();
    try {
        while ((n = inp.read(buf)) > 0) {
            sig.getSigner().update(buf, 0, n);
        }
    } catch (SignatureException se) {
        throw new ExceptionConverter(se);
    }
    buf = new byte[totalBuf];
    byte[] bsig = sig.getSignerContents();
    System.arraycopy(bsig, 0, buf, 0, bsig.length);
    PdfString str = new PdfString(buf);
    str.setHexWriting(true);
    PdfDictionary dic = new PdfDictionary();
    dic.put(PdfName.CONTENTS, str);
    sigApp.close(dic);
    stamper.reader.close();
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) InputStream(java.io.InputStream) SignatureException(java.security.SignatureException)

Example 25 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class PdfWriter method addSimplePattern.

PdfName addSimplePattern(PdfPatternPainter painter) {
    PdfName name = (PdfName) documentPatterns.get(painter);
    try {
        if (name == null) {
            name = new PdfName("P" + patternNumber);
            ++patternNumber;
            documentPatterns.put(painter, name);
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
    return name;
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) IOException(java.io.IOException) DocumentException(pdftk.com.lowagie.text.DocumentException)

Aggregations

ExceptionConverter (pdftk.com.lowagie.text.ExceptionConverter)46 IOException (java.io.IOException)34 DocumentException (pdftk.com.lowagie.text.DocumentException)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 SignatureException (java.security.SignatureException)5 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchProviderException (java.security.NoSuchProviderException)4 CRLException (java.security.cert.CRLException)4 CertificateException (java.security.cert.CertificateException)4 ArrayList (java.util.ArrayList)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 ASN1OutputStream (org.bouncycastle.asn1.ASN1OutputStream)3 DEROctetString (org.bouncycastle.asn1.DEROctetString)3 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)3 Color (java.awt.Color)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2