2016-08-18 43 views
0

我正尝试在我的android应用程序中使用www.itextpdf.com创建波斯 pdf文件,但是我找不到java.io.IOException arial.ttf作为文件或资源。这是有问题的代码。android itext在Android Studio中使用资产字体

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

这段代码很好eclipse(java项目不是android),但不会在Android studio中工作。我不知道如何解决ttf文件。任何帮助,将不胜感激。

回答

0

这很明显:Android没有Arial字体。 您必须为您的脚本(波斯语)随应用程序一起提供适当的字体,并从资源/资产/等中读取字体。参考How to retrieve a list of available/installed fonts in android?

UPD:

一旦你的资产文件,通过抓住一个getAssets()AssetManager用它来读取字体的字节数。 This解答可能是有用的。

之后,你可以自由地创建您的字体是这样的:

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

同时,我强烈建议你上附带的Windows字体的许可仔细阅读,以确定你是否能够复制 - 将Arial字体粘贴到您的应用程序中,然后像这样使用它(我怀疑是这样)。

+0

是的我知道这一点,我添加到/ assets /字体arial.ttf,但我得到java.io.IOException问题是如何解决该arial.ttf文件?在eclipse中的一个简单的java类中,我将arial.ttf添加到src,它可以工作,但在android studio中它不会。 –

+0

@MohamadSharifi,我已通过回答 –

+0

更新请问您如何从字体中读取字节?你能给我更详细的帮助吗?谢谢 –

0

到这里看看:

属性

customAttrs:fontType="free_mono" 

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"/> 

有关更多信息,请参见本link

+0

谢谢你的回答。我只想解决一个字体文件,以便我可以使用它来使用iText生成PDF文件。 –

相关问题