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

Hi @anatoly.sidorenko ,

my company bought the license and I’d like to replace my ‘hacky’ version. How would I request a specific version of Aspose.Word.JasperReports, where Arial is the default style in Blank.docx?

I would need the version 23.10.0.

Thanks for your help
Stefan

Hi @sduffner1

I finished WORDSJR-301 and updated Blank.doc in 25.1 release.
Can you download and check, is new template work right?

Usually we never changed files in older version. Why you need 23.10.0 ?
If it necessary I will repackage files of version 23.10 with new Blanck.doc and publish it as 23.10.1

Hi @anatoly.sidorenko ,

first of all, thank you.

I’m going to try it out. But I’m not very optimistic, because any newer version than 23.10.0 introduced a huge exception, because we’re still using JasperReports 6.8.0.

I’ll keep you posted.

1 Like

Hi @sduffner1

I finished 23.10.1 release and you can find it on this link:
https://releases.aspose.com/words/jasperreports/new-releases/aspose.words-for-jasperreports-23.10.1/

Hi @anatoly.sidorenko,

this morning I could spend some time on testing your releases. Unfortunately, both version didn’t work.

The 25.1 release didn’t work at all, as expected, but this seems to be a completely different problem. I may open a different thread.

The 23.10.1 release worked, but Times New Roman is still the default font. What I analyzed so far is the following. I extracted Blank.doc from your release, opened the Word document and Arial is the default font in the standard template. Looks good, but if I select another Word template like ‘title’ and select the standard template again the default font is now Times New Roman. In my opinion this seems to be the problem.

I’m not an Word expert, I probably did something differently when I changed the Word document. Unfortunately, I don’t remember exactly what I modified. I upload my version of the Blank.doc, maybe you have an idea what the difference is.
Blank.zip (5.3 KB)

Stefan

@sduffner1 Thank you for the additional information. Anatoly is currently on vacation.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSJR-390

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi @sduffner1

I rebuild 23.10.1 release with your Blank.doc and you can find it on this link:
https://releases.aspose.com/words/jasperreports/new-releases/aspose.words-for-jasperreports-23.10.1/