Search in sources :

Example 6 with ExceptionConverter

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

the class ColumnText method showTextAligned.

/**
 * Shows a line of text. Only the first line is written.
 * @param canvas where the text is to be written to
 * @param alignment the alignment. It is not influenced by the run direction
 * @param phrase the <CODE>Phrase</CODE> with the text
 * @param x the x reference position
 * @param y the y reference position
 * @param rotation the rotation to be applied in degrees counterclockwise
 * @param runDirection the run direction
 * @param arabicOptions the options for the arabic shaping
 */
public static void showTextAligned(PdfContentByte canvas, int alignment, Phrase phrase, float x, float y, float rotation, int runDirection, int arabicOptions) {
    if (alignment != Element.ALIGN_LEFT && alignment != Element.ALIGN_CENTER && alignment != Element.ALIGN_RIGHT)
        alignment = Element.ALIGN_LEFT;
    canvas.saveState();
    ColumnText ct = new ColumnText(canvas);
    if (rotation == 0) {
        if (alignment == Element.ALIGN_LEFT)
            ct.setSimpleColumn(phrase, x, y - 1, 20000 + x, y + 2, 2, alignment);
        else if (alignment == Element.ALIGN_RIGHT)
            ct.setSimpleColumn(phrase, x - 20000, y - 1, x, y + 2, 2, alignment);
        else
            ct.setSimpleColumn(phrase, x - 20000, y - 1, x + 20000, y + 2, 2, alignment);
    } else {
        double alpha = rotation * Math.PI / 180.0;
        float cos = (float) Math.cos(alpha);
        float sin = (float) Math.sin(alpha);
        canvas.concatCTM(cos, sin, -sin, cos, x, y);
        if (alignment == Element.ALIGN_LEFT)
            ct.setSimpleColumn(phrase, 0, -1, 20000, 2, 2, alignment);
        else if (alignment == Element.ALIGN_RIGHT)
            ct.setSimpleColumn(phrase, -20000, -1, 0, 2, 2, alignment);
        else
            ct.setSimpleColumn(phrase, -20000, -1, 20000, 2, 2, alignment);
    }
    if (runDirection == PdfWriter.RUN_DIRECTION_RTL) {
        if (alignment == Element.ALIGN_LEFT)
            alignment = Element.ALIGN_RIGHT;
        else if (alignment == Element.ALIGN_RIGHT)
            alignment = Element.ALIGN_LEFT;
    }
    ct.setAlignment(alignment);
    ct.setArabicOptions(arabicOptions);
    ct.setRunDirection(runDirection);
    try {
        ct.go();
    } catch (DocumentException e) {
        throw new ExceptionConverter(e);
    }
    canvas.restoreState();
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) DocumentException(pdftk.com.lowagie.text.DocumentException)

Example 7 with ExceptionConverter

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

the class DefaultFontMapper method awtToPdf.

/**
 * Returns a BaseFont which can be used to represent the given AWT Font
 *
 * @param	font		the font to be converted
 * @return	a BaseFont which has similar properties to the provided Font
 */
public BaseFont awtToPdf(Font font) {
    try {
        BaseFontParameters p = getBaseFontParameters(font.getFontName());
        if (p != null)
            return BaseFont.createFont(p.fontName, p.encoding, p.embedded, p.cached, p.ttfAfm, p.pfb);
        String fontKey = null;
        String logicalName = font.getName();
        if (logicalName.equalsIgnoreCase("DialogInput") || logicalName.equalsIgnoreCase("Monospaced") || logicalName.equalsIgnoreCase("Courier")) {
            if (font.isItalic()) {
                if (font.isBold()) {
                    fontKey = BaseFont.COURIER_BOLDOBLIQUE;
                } else {
                    fontKey = BaseFont.COURIER_OBLIQUE;
                }
            } else {
                if (font.isBold()) {
                    fontKey = BaseFont.COURIER_BOLD;
                } else {
                    fontKey = BaseFont.COURIER;
                }
            }
        } else if (logicalName.equalsIgnoreCase("Serif") || logicalName.equalsIgnoreCase("TimesRoman")) {
            if (font.isItalic()) {
                if (font.isBold()) {
                    fontKey = BaseFont.TIMES_BOLDITALIC;
                } else {
                    fontKey = BaseFont.TIMES_ITALIC;
                }
            } else {
                if (font.isBold()) {
                    fontKey = BaseFont.TIMES_BOLD;
                } else {
                    fontKey = BaseFont.TIMES_ROMAN;
                }
            }
        } else {
            if (font.isItalic()) {
                if (font.isBold()) {
                    fontKey = BaseFont.HELVETICA_BOLDOBLIQUE;
                } else {
                    fontKey = BaseFont.HELVETICA_OBLIQUE;
                }
            } else {
                if (font.isBold()) {
                    fontKey = BaseFont.HELVETICA_BOLD;
                } else {
                    fontKey = BaseFont.HELVETICA;
                }
            }
        }
        return BaseFont.createFont(fontKey, BaseFont.CP1252, false);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter)

Example 8 with ExceptionConverter

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

the class PdfEncodings method convertToBytes.

/**
 * Converts a <CODE>String</CODE> to a </CODE>byte</CODE> array according
 * to the font's encoding.
 * @return an array of <CODE>byte</CODE> representing the conversion according to the font's encoding
 * @param encoding the encoding fo the return byte array
 * @param text the <CODE>String</CODE> to be converted
 */
public static final byte[] convertToBytes(String text, String encoding) {
    if (text == null)
        return new byte[0];
    if (encoding == null || encoding.length() == 0) {
        int len = text.length();
        byte[] b = new byte[len];
        for (int k = 0; k < len; ++k) {
            b[k] = (byte) text.charAt(k);
        }
        return b;
    }
    ExtraEncoding extra = null;
    synchronized (extraEncodings) {
        extra = (ExtraEncoding) extraEncodings.get(encoding.toLowerCase());
    }
    if (extra != null) {
        byte[] b = extra.charToByte(text, encoding);
        if (b != null)
            return b;
    }
    IntHashtable hash = null;
    if (encoding.equals(BaseFont.WINANSI))
        hash = winansi;
    else if (encoding.equals(PdfObject.TEXT_PDFDOCENCODING))
        hash = pdfEncoding;
    if (hash != null) {
        char[] cc = text.toCharArray();
        int len = cc.length;
        int ptr = 0;
        byte[] b = new byte[len];
        int c = 0;
        for (int k = 0; k < len; ++k) {
            char char1 = cc[k];
            if (char1 < 128 || (char1 >= 160 && char1 <= 255))
                c = char1;
            else
                c = hash.get(char1);
            if (c != 0)
                b[ptr++] = (byte) c;
        }
        if (ptr == len)
            return b;
        byte[] b2 = new byte[ptr];
        System.arraycopy(b, 0, b2, 0, ptr);
        return b2;
    }
    if (encoding.equals(PdfObject.TEXT_UNICODE)) {
        // workaround for jdk 1.2.2 bug
        char[] cc = text.toCharArray();
        int len = cc.length;
        byte[] b = new byte[cc.length * 2 + 2];
        b[0] = -2;
        b[1] = -1;
        int bptr = 2;
        for (int k = 0; k < len; ++k) {
            char c = cc[k];
            b[bptr++] = (byte) (c >> 8);
            b[bptr++] = (byte) (c & 0xff);
        }
        return b;
    }
    try {
        return text.getBytes(encoding);
    } catch (UnsupportedEncodingException e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 9 with ExceptionConverter

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

the class PdfEncryption method encryptByteArray.

public byte[] encryptByteArray(byte[] b) {
    try {
        ByteArrayOutputStream ba = new ByteArrayOutputStream();
        OutputStreamEncryption os2 = getEncryptionStream(ba);
        os2.write(b);
        os2.finish();
        return ba.toByteArray();
    } catch (IOException ex) {
        throw new ExceptionConverter(ex);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 10 with ExceptionConverter

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

the class OutputStreamEncryption method finish.

public void finish() throws IOException {
    if (!finished) {
        finished = true;
        if (aes) {
            byte[] b;
            try {
                b = cipher.doFinal();
            } catch (Exception ex) {
                throw new ExceptionConverter(ex);
            }
            out.write(b, 0, b.length);
        }
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) IOException(java.io.IOException)

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