0

I'm trying to create Persian pdf files in my android application using www.itextpdf.com but I get java.io.IOException arial.ttf not found as file or resource. Here is the code with the problem.

BaseFont font = BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

this code is fine in eclipse(java project not android) but wont work in Android studio. I don't know how to address the ttf file. Any help would be appreciated.

Amir
  • 16,067
  • 10
  • 80
  • 119

2 Answers2

0

This is obvious: Android does not have Arial font. You have to ship the appropriate font for your script (Persian) along with your application and read the font from resources/assets/etc.

See also How to retrieve a list of available/installed fonts in android?.

UPD:

Once you have your file in assets, grab an AssetManager via getAssets() and use it to read the bytes from the font. This answer might be useful.

Afterwards you are free to create your font like this:

BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, true, false, bytes, null);

Meanwhile, I strongly encourage you to read thoroughly on the licensing of the fonts that are shipped with Windows to determine whether you are able to copy-paste Arial font into your application and then use it like this (I doubt so).

Community
  • 1
  • 1
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
  • Yes I know that and I added arial.ttf to /assets/fonts but I get java.io.IOException the question is how to address that arial.ttf file? In a simple java class in eclipse I add the arial.ttf to src and it works but in android Studio it doesn't. – Mohamad Sharifi Aug 18 '16 at 21:17
  • Would you please tell me how to read bytes from the font? Can you give me more detailed help? Thanks – Mohamad Sharifi Aug 18 '16 at 22:20
  • You need the first 5 lines from [this](http://stackoverflow.com/a/14515873/2333458) answer. – Alexey Subach Aug 18 '16 at 22:49
  • Still got the problem here is the code: InputStream is = getAssets().open("/assets/fonts/arial.ttf"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); BaseFont base=BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, true, false, buffer, null); – Mohamad Sharifi Aug 18 '16 at 23:49
  • have you tested the code? Am I doing something wrong? – Mohamad Sharifi Aug 18 '16 at 23:53
  • SO is not a place where the community writes the code for you. [Here](http://stackoverflow.com/a/14515590/2333458), which is by the way one of the links I gave you, it is stated in black and white, that if you put your file `abc.txt` into `assets/abc.txt`, then you should access it as `open("abc.txt")`, not `open("assets/abc.txt")`. – Alexey Subach Aug 19 '16 at 06:24
0

Take a look here:

Attribute

customAttrs:fontType="free_mono"

The TextView

<com.seamusdawkins.fontcustom.CustomTextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="@string/str_text_default"
        android:textSize="30dp"
        customAttrs:fontType="free_mono"
        android:gravity="center"/>

See this link for more information.

viana
  • 495
  • 4
  • 18
  • 42