如何从Android的Font文件夹中获取tff字体文件

huangapple go评论76阅读模式
英文:

How to get tff font file from Font folder in android

问题

我正在Android中创建一个PDF文件,其中包含不同的语言。我在res目录下的font文件夹中有notoserifdevanagaribold.tff,但我无法在字符串变量中访问它。我正在使用iText7来创建PDF文件。在iText7中,PdfFontFactory.createFont(String FONT, PdfEncodings.IDENTITY_H); 需要一个字符串值作为字体路径。如果我将***/font/notoserifdevanagaribold*** 放入字符串变量中,我会得到以下错误信息。

W/System.err: com.itextpdf.io.IOException: Font file font/notoserifdevanagaribold.ttf not found.
        at com.itextpdf.io.font.FontProgram.checkFilePath(FontProgram.java:284)
        at com.itextpdf.io.font.TrueTypeFont.<init>(TrueTypeFont.java:91)
        at com.itextpdf.io.font.FontProgramFactory.createFont(FontProgramFactory.java:206)
W/System.err:     at com.itextpdf.io.font.FontProgramFactory.createFont(FontProgramFactory.java:115)

请问有人可以告诉我如何解决这个问题吗?下面是我代码的截图。

如何从Android的Font文件夹中获取tff字体文件

英文:

i am creating a pdf file in android in which different languages are there. i am having notoserifdevanagaribold.tff in my font folder under res directory but i am not able to access it in a string variable. i am using itext7 to create a pdf file. in itext7 PdfFontFactory.createFont(String FONT, PdfEncodings.IDENTITY_H); requires string value as a font path. if i am putting /font/notoserifdevanagaribold in string variable i am getting below error.

W/System.err: com.itextpdf.io.IOException: Font file font/notoserifdevanagaribold.ttf not found.
        at com.itextpdf.io.font.FontProgram.checkFilePath(FontProgram.java:284)
        at com.itextpdf.io.font.TrueTypeFont.&lt;init&gt;(TrueTypeFont.java:91)
        at com.itextpdf.io.font.FontProgramFactory.createFont(FontProgramFactory.java:206)
W/System.err:     at com.itextpdf.io.font.FontProgramFactory.createFont(FontProgramFactory.java:115)

can some please let me know how to do that. below i have attached screenshot of my code.

如何从Android的Font文件夹中获取tff字体文件 FONT = R.font.notoserifdevanagaribold;

答案1

得分: 2

正如André在评论中正确指出的那样,您应该使用Android的AssetManager来获取资源。原始异常仅因Android上的文件访问与其他系统不同而引发。以下是获取流、将其转换为字节数组并将字节传递给iText的方法:

AssetManager am = this.getAssets();
try (InputStream inStream = am.open("notoserifdevanagaribold.ttf")) {
    byte[] fontData = IOUtils.toByteArray(inStream);
    PdfFont font = PdfFontFactory.createFont(fontData, PdfEncodings.IDENTITY_H);
}

这里我使用了Apache Commons IO库从流中获取字节,您可以将其作为Maven依赖项添加如下:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
英文:

As André correctly pointed out in the comment, you should use Android's AssetManager to fetch resources. The original exception just comes from the fact that file access differs from other systems on Android. Here is how you should get the stream, convert it into byte array and pass bytes to iText:

AssetManager am = this.getAssets();
try (InputStream inStream = am.open(&quot;notoserifdevanagaribold.ttf&quot;)) {
    byte[] fontData = IOUtils.toByteArray(inStream);
    PdfFont font = PdfFontFactory.createFont(fontData, PdfEncodings.IDENTITY_H);
}

Here I'm using Apache Commons IO library to get bytes from the stream which you can add as a Maven dependency in the following way:

&lt;dependency&gt;
    &lt;groupId&gt;commons-io&lt;/groupId&gt;
    &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
    &lt;version&gt;2.6&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年1月3日 15:30:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定