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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With