How to print indian regional language in java like gujarati?

436 Views Asked by At

I am try to give gujarati print on printer but it shows me square boxes on page like in following.

enter image description here

I had also print on console in netbeas but still result are same. I had also tried Java UTF-8 encoding and decoding using following code but not get expected output.

 String s = "શ્રેષ્ઠ અહેવાલ એન્જિન ઉપલબ્ધ એફ";
 byte arr[] = s.getBytes("UTF8");
 System.out.println(new String(arr, "UTF-8"));

output:

enter image description here

i had used following code for print :

class print{    
    public static void main(Strng args[]){
        try{
        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setPrintable(new BillPrintable(), BillPrintable.getPageFormat(pj));
        pj.print();
        catch(Exception e){}
    }
        
        
 class BillPrintable implements Printable {

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
        throws PrinterException {

    int result = NO_SUCH_PAGE;
    if (pageIndex == 0) {

        Graphics2D g2d = (Graphics2D) graphics;
        
        g2d.drawString("શ્રેષ્ઠ અહેવાલ એન્જિન ઉપલબ્ધ એફ", 0, 20);
        
        result = PAGE_EXISTS;
    }
    return result;
   }

public static PageFormat getPageFormat(PrinterJob pj) {

    PageFormat pf = pj.defaultPage();
    Paper paper = pf.getPaper();

    double middleHeight = 10.0;
    double headerHeight = 1.0;
    double footerHeight = 1.0;
    double width = convert_CM_To_PPI(8);      //printer know only point per inch.default value is 72ppi
    double height = convert_CM_To_PPI(headerHeight + middleHeight + footerHeight);
    paper.setSize(width, height);
    paper.setImageableArea(
            0,
            0,
            width,
            height - convert_CM_To_PPI(1)
    );   //define boarder size    after that print area width is about 180 points

    pf.setOrientation(PageFormat.PORTRAIT);           //select orientation portrait or landscape but for this time portrait
    pf.setPaper(paper);

    return pf;
}

protected static double convert_CM_To_PPI(double cm) {
    return toPPI(cm * 0.393600787);
}
}
}

so please help me to ride off this problem and thanks in advance.

2

There are 2 best solutions below

0
Prashant Sonar On BEST ANSWER
 URL fontUrl = new File("....").toURI().toURL();
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
            font = font.deriveFont(Font.PLAIN, 11);

Using above code I had solved issue and thanks @joni for your hint in comment regarding font.

issue was regarding font I had downloaded regional language font .ttf file and used it in project using above code.

0
user13784117 On

Works for me.

Output:

$ java Foo
શ્રેષ્ઠ અહેવાલ એન્જિન ઉપલબ્ધ એફ

Code:

class Foo {
    public static void main(String... args) {
        System.out.println("શ્રેષ્ઠ અહેવાલ એન્જિન ઉપલબ્ધ એફ");
    }
}
    

The same output is obtained on paper from my HP LaserJet printer.

It's just a matter of having a terminal or printer that understands the character set (likely UTF-8) and a font that contains the characters you need. The program is not the problem.

In my case I'm connected to a Raspberry Pi running Raspbian. My terminal emulator understands UTF-8. I have no idea what font I'm using.