> -----Original Message-----
> From: Matthias Rueggeberg [SMTP:[EMAIL PROTECTED]]
> Sent: Monday, September 30, 2002 14:38
> To:   [EMAIL PROTECTED]
> Subject:      [iText-questions] Using ttf-fonts in *.war files
> 
> Hi,
>  
> I'm trying to use a ttf-font that is included into a war-file.
> Unfortunately all methods of the itext-library expect a file name to the
> ttf-font on a real file system.
> 
        That's not true, you can also provide the font in a byte array.

> Is there a known way to read fonts that are included into a war-file?
> 
        Of course there is. Put the font some place that java can get. Let's
suppose that the font is arial.ttf and is in the package mypackage.fonts.

        1 - create this method somewhere (the method is similar to the one
in BaseFont but it's NOT the same).

            public static InputStream getResourceStream(String key) {
                InputStream is = null;
                // Try to use Context Class Loader to load the properties
file.
                try {
                    java.lang.reflect.Method getCCL =
                        Thread.class.getMethod("getContextClassLoader", new
Class[0]);
                    if (getCCL != null) {
                        ClassLoader contextClassLoader =
        
(ClassLoader)getCCL.invoke(Thread.currentThread(),
                                                       new Object[0]);
                        is = contextClassLoader.getResourceAsStream(key);
                    }
                } catch (Exception e) {}

                if (is == null) {
                    is = BaseFont.class.getResourceAsStream(key);
                }
                return is;
            }

        2 - read the font.

        InputStream is = getResourceStream("/mypackage/fonts/arial.ttf");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        while (true) {
            int size = is.read(buf);
            if (size < 0)
                break;
            out.write(buf, 0, size);
        }
        is.close();
        buf = out.toByteArray();
        BaseFont bf = BaseFont.createFont("arial.ttf", BaseFont.CP_1252,
true, true, buf, null);

        Best Regards,
        Paulo Soares
>  
> Thanks,
> Matthias
> 
> 
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> iText-questions mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/itext-questions


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to