3

My tool basically read the PDF and print the pdf's content in JTextArea. Everything working fine until my PDF contains PH Mirjan fonts for Arabic. My text area shows some junk character as per below.

enter image description here

How do I solve this?

My default font for the text area is Arial Unicode MS. Is there anyway I can configure the text area's font to take? Let say I downloaded PH Mirjan in my local, how to change the text area font to the downloaded one. Any advice or references links is highly appreciated.

EDIT

try (InputStream is = NewJFrame.class.getResourceAsStream("/GE SS Two Bold.otf")) 
        {
            Font font = Font.createFont(Font.TRUETYPE_FONT, is);
            font = font.deriveFont(Font.PLAIN, 24f);
            jTextArea1.setFont(font);
            jTextArea1.setForeground(Color.BLUE);

and its give me this Exception.

Exception

java.awt.FontFormatException: java.nio.BufferUnderflowException at sun.font.TrueTypeFont.init(TrueTypeFont.java:558) at sun.font.TrueTypeFont.(TrueTypeFont.java:191) at sun.font.CFontManager.createFont2D(CFontManager.java:161) at java.awt.Font.(Font.java:614) at java.awt.Font.createFont0(Font.java:968) at java.awt.Font.createFont(Font.java:876)

Any idea why i'm getting this.?

chinna_82
  • 6,353
  • 17
  • 79
  • 134
  • 1
    `JTextArea#setFont`, but you need to use something like [`Font.createFont`](http://docs.oracle.com/javase/7/docs/api/java/awt/Font.html#createFont(int,%20java.io.File)) to load it first (assuming it's not loaded in the system fonts of the OS), otherwise you should be able to create it by name. For [example](http://stackoverflow.com/questions/24177348/font-awesome-with-swing/24177458#24177458) and [example](http://stackoverflow.com/questions/27809337/java-fonts-not-displaying-properly/27809588#27809588) – MadProgrammer Jun 30 '15 at 03:54
  • 1
    or [example](http://stackoverflow.com/questions/24278648/how-to-change-font-in-java-gui-application/24278825#24278825) – MadProgrammer Jun 30 '15 at 03:57
  • 1
    Hopefully, this [another example](http://stackoverflow.com/a/9651404/1057230), will also help you in your good endeavour :-) – nIcE cOw Jun 30 '15 at 04:00
  • 2
    ..or [example](http://stackoverflow.com/a/13718134/418556) or [example](http://stackoverflow.com/a/6965149/418556) .. I'm pretty sure this is a duplicate of *some* exact question.. – Andrew Thompson Jun 30 '15 at 05:35

1 Answers1

1

Why is the font name is .tt instead of .ttf ? The case might be it is not a ttf file or corrupted that the exception happens

    try {
        Font NARROW = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("/fonts/DSS.ttf"));   
        NARROW = NARROW.deriveFont(17f);       
    } catch (FontFormatException | IOException ex) {
        System.err.println("Exception loading fonts "+ex);
    }

I know it is pretty much the same code , try this on other ttf files. Exception should not be there.

Rahul
  • 289
  • 2
  • 7