Jump to content
We've recently updated our Privacy Statement, available here ×

How to set a new font at runtime


raphaeltmolnar

Recommended Posts

Hello everyone,

I've been strugling with a pdf i have to generate in greek. As you may immagine not every font has greek letters so I had to embbed my own in the project and this is where my issu began.

I'm developing for an OSGI environment so I tried to add my generated jar from jasperSoft studio into the application classpath, once I've wasted many hours trying to make it work I found this post on stack overflow:

https://stackoverflow.com/questions/57918748/how-to-programmatically-load-font-extension-at-runtime

The important part is that we could create a Jasper context of our own and add the font there:

SimpleJasperReportsContext jasperReportsContext = new SimpleJasperReportsContext();SimpleFontFamily fontFamily = new SimpleFontFamily(jasperReportsContext);fontFamily.setName("family name");//to be used in reports as fontNamefontFamily.setPdfEmbedded(true);fontFamily.setPdfEncoding("Identity-H");SimpleFontFace regular = new SimpleFontFace(jasperReportsContext);regular.setTtf("font ttf path");fontFamily.setNormalFace(regular);jasperReportsContext.setExtensions(FontFamily.class, Arrays.asList(fontFamily));//use the context when filling and exporting reports//note that there are variations here depending on the API you use for filling and exportingJasperPrint jasperPrint = JasperFillManager.getInstance(jasperReportsContext).fill(jasperReport, params);...JasperExportManager.getInstance(jasperReportsContext).exportToPdf(jasperPrint);[/code]

This seemed a good path to follow but now i'm not entirely sure it's possible to add a font in runtime. Inside the jasperReports I've found the method used to fill the report:

    /**     * @see #fill(InputStream, Map, JRDataSource)     */    public static JasperPrint fillReport(        InputStream inputStream,         Map<String,Object> parameters,        JRDataSource dataSource        ) throws JRException    {        return getDefaultInstance().fill(inputStream, parameters, dataSource);    }[/code]

Which means that it will alway ignore my custom instance with my jasperReportContext and will create a new one with it's default context.

Having said all that, I would like to know if there is any wsay to achive my goal of registering a font (ttf) programatically / in run time

Best Regards 

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...