gobenn71 Posted January 8, 2010 Share Posted January 8, 2010 Hi, is there a way to register PDF fonts for Jasper Reports at runtime of an application ?Background is that our application is running 24-7 but users should be capable of deploying new reports at runtime. These reports may include fonts which are not in the classpath, so is there a possibility to register new fonts at runtime ? Link to comment Share on other sites More sharing options...
gobenn71 Posted January 8, 2010 Author Share Posted January 8, 2010 I have another question, how do i retrieve the (PDF-)fonts which are available in my application at runtime ? Link to comment Share on other sites More sharing options...
pablodc Posted January 8, 2010 Share Posted January 8, 2010 Hi, I'm using this method (fired by Quartz) to register new fonts (I'dont know if this method is compatible with the last release of JR): I also have a method to list the fonts. Hope this helpsGood luck Code: public static HashMap<FontKey,PdfFont> fontMap = new HashMap<FontKey,PdfFont>(); private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); /** * * Metodo para registrar las tipografias de la aplicacion en el sistema de * fuentes de AWT y dentro de las tipografias disponibles para la libreria * iText, utilizada por JasperReports para la exportacion de reportes en * formato PDF. * * @param fontfile nombre del archivo ttf de la tipografia que se desea * registrar. * @return devuelve un <code>boolean</code> indicando si se pudo * o no registrar la tipografia */ private boolean registerFont(String fontsdir, String fontfile) { /** registramos una tipografia propia en el sistema de tipografias de AWT/JAVA 2D con esto la ponemos disponible para que JasperReports pueda realizar sus calculos de altura y dimesiones para la impresion del documento. */ FileInputStream fis = null; boolean retval = false; String fullfontpath = fontsdir + File.separator + fontfile; String fontthumb = fontsdir + File.separator + FilenameUtils.getBaseName(fontfile) + "." + FontThumbs.IMAGEFORMATEXTENSION; FontKey key; PdfFont font; try { _log.info(sessionid +"registrando en AWT"); fis = new FileInputStream(fullfontpath); Font customFont = Font.createFont(Font.TRUETYPE_FONT, fis); ge.registerFont(customFont); _log.info(sessionid +"registrando en iText"); FontFactory.register(fullfontpath); _log.info(sessionid + "se logro registrar la tipografia <" + customFont.getFontName() + "> correspondiente a la familia <" + customFont.getFamily() + ">"); /** * actualizamos el parametro FONT_MAP que luego utilizara la clase * ReportExporter al exportar el reporte al formato PDF. */ key = new FontKey(customFont.getFontName(), false, false); font = new PdfFont(fullfontpath, "Cp1252", true); fontMap.put(key, font); retval = true; } catch (FontFormatException ex) { _log.error(sessionid + "se produjo un error al registrar la " + "tipografia <" + ex + ">"); ex.printStackTrace(); } catch (IOException ex) { _log.error(sessionid + "se produjo un error de E/S <" + ex + ">"); ex.printStackTrace(); } catch (Exception e) { _log.error(sessionid + "se produjo un error generico <" + e + ">"); e.printStackTrace(); } finally { try { fis.close(); } catch (IOException ex) { _log.error(sessionid + "se produjo un error al cerrar el " + "inputstream de la tipografia <" + ex + ">"); ex.printStackTrace(); } } return retval; } /** * metodo para listar las tipografias registradas en el sistema de AWT y * en la libreria iText que utiliza JasperReports para exportar los * reportes a PDF. */ public static void listFonts() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); _log.debug("=== inicio listado de tipografias del sistema ===>"); String[] fontNames = ge.getAvailableFontFamilyNames(); Font[] fontfiles = ge.getAllFonts(); for (int index = 0; index < fontNames.length; index++) { _log.debug("\t" + fontNames[index]); } _log.debug("\t*********************************************"); for (int index = 0; index < fontfiles.length; index++) { _log.debug("\t" + fontfiles[index].getFontName()); } _log.debug("<=== fin listado de tipografias del sistema ==="); _log.debug( "--- inicio listado de tipografias disponibles para iText --->"); for (Iterator i = FontFactory.getRegisteredFonts().iterator(); i.hasNext();) { _log.debug("\t" + (String) i.next()); } _log.debug( "<--- fin listado de tipografias disponibles para iText ---"); } Link to comment Share on other sites More sharing options...
gobenn71 Posted January 8, 2010 Author Share Posted January 8, 2010 @pablo : Thanks alot. I tested your method to retrieve the installed fonts but it doesn't return fonts which are located in the classpath of my application.anyway, thank you. Link to comment Share on other sites More sharing options...
pablodc Posted January 15, 2010 Share Posted January 15, 2010 Maybe this code helps: import java.io.File; /** @author Ram, Matthews */public class Main { public static void main(final String[] args) throws Throwable {final String pathSep = System.getProperty("path.separator");final String list = System.getProperty("java.class.path");for (final String path : list.split(pathSep)) {final File object = new java.io.File(path);if( object.isDirectory()) ls(object);else System.out.println(object);}}/** list recursively */private static void ls(File f) { File list[] = f.listFiles();for (File file : list) {if (file.isDirectory()) ls(file);else System.out.println(file);}}} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now