Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anything for deciding default paper size based on locale?

Tags:

java

locale

Within Java, is there anything that I can use out of box to decide the default paper size based on locale?

I am building a web application and need to generate a PDF document. Its size can be letter, legal, A4, etc., depending on the locale. From the web application, I can get the visitor's locale info such as country, etc.

like image 839
curious1 Avatar asked Nov 01 '22 16:11

curious1


1 Answers

java.awt.PageAttributes has a method called setMediaToDefault() which should do just that.

If you believe its javadoc:

The default size for locales in the United States and Canada is MediaType.NA_LETTER. The default size for all other locales is MediaType.ISO_A4.

So, if you want to steal borrow its implementation:

public void setMediaToDefault(){
    String defaultCountry = Locale.getDefault().getCountry();
    if (defaultCountry != null &&
        (defaultCountry.equals(Locale.US.getCountry()) ||
         defaultCountry.equals(Locale.CANADA.getCountry()))) {
        setMedia(MediaType.NA_LETTER);
    } else {
        setMedia(MediaType.ISO_A4);
    }
}
like image 92
Mureinik Avatar answered Nov 11 '22 14:11

Mureinik