Jure Mali
Jure Mali

Reputation: 56

Use Custom font with Unity3D and ItextSharp

First of all, this question is copied from Unity Answers with no luck getting any solution. If this approach is not acceptable just let me know.

Because I need to use special characters, integrated itextsharp font cant handle them, I need to use my own custom font. The integration of custom font in my app is working in Editor but in Android device not.

Does any body know how to access font on android for use with itextsharp?

For my font path I'm using:

 string fontPath = Application.dataPath + "/" + "FreeSans" + ".ttf";

i tried also:

string fontPath = Application.persistentDataPath + "/" + "FreeSans" + ".ttf";

And for font creation I use:

BaseFont bf = BaseFont.CreateFont(fontPath , BaseFont.CP1250, true);

When I use first option it works only in Unity Editor.

I also tried with no luck this two options. One path goes directly to streaming assets folder located in apk file and the other one is located on android folder device, where font was copied from streaming assets folder. Every time, PDF was canceled in the middle and the process was stopped.

path = "jar:" + Application.dataPath + "!/assets/" + "FreeSans" + ".ttf";

 path = System.IO.Path.Combine(Application.streamingAssetsPath, "FreeSans.ttf");

itextsharp is behaving really odd in case when you are making PDF on android.

Does any body know how to access system font from android system, to locate font on device. Maybe itextsharp could manage this kind of path?

Thank you in advance.

J.

Upvotes: 1

Views: 903

Answers (1)

Jure Mali
Jure Mali

Reputation: 56

So. After hours of scratching my head I finally got it right.

The first part of solution is written here. You have to put some DLL files to project folder. Which one depends what encoding you have to use.

http://answers.unity3d.com/questions/42955/codepage-1252-not-supported-works-in-editor-but-no.html

The second part goes like this:

First of all you have to make path to Android system font directory with font name:

path = Path.Combine("/system/fonts/", "DroidSans.ttf");

Register that font with FontFactory:

iTextSharp.text.FontFactory.Register(path, "DroidSans");

After that, you just declare font with FontFactory.GetFont:

font = iTextSharp.text.FontFactory.GetFont("DroidSans", 
                                BaseFont.IDENTITY_H,
                                false, 
                                10, 
                                iTextSharp.text.Font.NORMAL);

And thats it!

Upvotes: 2

Related Questions