Aspose Word JasperReports - Setting default locale and font

Hi,

I’m evaluating Aspose for JasperReports and we found two small problems. I think, that these may be solved easily.

  1. The default locale of the generated Word docx is ‘en-US’, but we need to change that to ‘de-DE’. I found this entry Set locale in Aspose.Words for Java/JasperReports, which didn’t work for us.

The sample code looks like this:

JasperPrint jasperPrint = createReport();
jasperPrint.setLocaleCode("de_DE");

ByteArrayOutputStream baos = new ByteArrayOutputStream();

JRAbstractExporter exporter = new AWDocxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
exporter.exportReport();

byte[] generatedWord = baos.toByteArray();
// write byte[] to file or stream via http
  1. The default font should be ‘Arial’, but the default is New Times Roman. How can we change this?
    The default JRStyle in my JasperReports template uses Arial as the default font, like
<style name="default" isDefault="true" isBlankWhenNull="true" fontName="Arial" fontSize="12"/>

The generate styles.xml looks like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:styles xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
          xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:docDefaults>
        <w:rPrDefault>
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:eastAsia="Times New Roman" w:hAnsi="Times New Roman"
                          w:cs="Times New Roman"/>
                <w:lang w:val="en-US" w:eastAsia="en-US" w:bidi="ar-SA"/>
            </w:rPr>
        </w:rPrDefault>
        <w:pPrDefault/>
    </w:docDefaults>
...

Thanks for any help

@sduffner1

To address the issues you’re facing with Aspose for JasperReports, here are some solutions for both changing the locale and setting the default font.

1. Changing the Locale to ‘de-DE’

To change the default locale of the generated Word document from ‘en-US’ to ‘de-DE’, you can set the locale code on the JasperPrint object before exporting. Your current implementation seems correct, but ensure that the locale is set properly. Here’s a refined version of your code:

JasperPrint jasperPrint = createReport();
jasperPrint.setLocaleCode("de-DE"); // Ensure the locale code is set correctly

ByteArrayOutputStream baos = new ByteArrayOutputStream();

JRAbstractExporter exporter = new AWDocxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
exporter.exportReport();

byte[] generatedWord = baos.toByteArray();
// write byte[] to file or stream via http

Make sure that the locale code is formatted correctly as “de-DE” (with a hyphen) instead of “de_DE” (with an underscore), as this can sometimes cause issues.

2. Setting the Default Font to ‘Arial’

To change the default font from ‘Times New Roman’ to ‘Arial’, you need to ensure that your JasperReports template is correctly configured. You mentioned that your JRStyle uses Arial, which is good. However, if the generated styles.xml still shows ‘Times New Roman’, you may need to explicitly set the font in your report generation code.

You can set the default font in the exporter settings like this:

AWDocxExporter exporter = new AWDocxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));

// Set default font
exporter.setConfiguration(new SimpleExporterConfiguration() {
    {
        setProperty("net.sf.jasperreports.export.docx.font.name", "Arial");
    }
});
exporter.exportReport();

This configuration should ensure that ‘Arial’ is used as the default font in the generated Word document.

Conclusion

By ensuring the locale is set correctly and explicitly configuring the default font in your exporter settings, you should be able to resolve the issues you’re encountering. If you continue to face problems, consider checking the documentation or community forums for additional support.

If you have any further questions or need more assistance, feel free to ask!

Hi @sduffner1

For your problem with default font I found similar code in our examples:

AWDocxExporter exporter = new AWDocxExporter();

HashMap fontMap = new HashMap();
fontMap.put("default", "Arial");
exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);

It should work with your JRStyle with name=“default”

Thanks for the answer, but the JRExporterParameter.FONT_MAP does not exist anymore in the JasperReports version 6.8.0 we’re using.

I’m setting the JasperReports’s default font in each JasperReport file, e.g.
<style name="default" isDefault="true" isBlankWhenNull="true" fontName="Arial" fontSize="12"/>.

Aspose is going to add a new style (in German this feature is called ‘Formatvorlagen’) named “default” to the created Word document. The default Word style is named ‘Standard’, which still uses ‘Times New Roman’ as its font. If I also name the JasperReport’s default style ‘Standard’, the created Word document has 2 styles one named ‘Standard’ and the other ‘Standard-1’.

I found this forum entry How to change Word Template in Jasper Library? - #13 by sduffner1, which described what I wanted to achive. The Word default style should use the font ‘Arial’. Therefore, a user, who starts to write a new paragraph uses the font ‘Arial’.

To my understanding a solution whould be to replace the default Word template ‘Blank.docx’ in the Aspose library. But the issue WORDSJR-301 is still open.

For any one having the same problem, what I tried to do:

  • I extracted the Word template ‘Blank.docx’ and set the font Arial to the Word’s style ‘Standard’
    • Add this modified to the Java classpath, which didn’t work
    • Re-packaged the jar file with the modified file, which didn’t work, had the same problem as in the other thread
    • Re-packaged the jar without the META-INF folder that worked. But I’m not sure if this is healthy technical solution and if the license is allowing this?

Hi @sduffner1

We build different version of Aspose.Words specially for JasperReports.
If Jasper Default style is Arial we can change Blank.docx in our jar and publish it with Arial font.

That sounds great :slight_smile: . Thank you, appreciate it. That makes my life much easier to convince my boss to buy the license.

1 Like