英文:
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)
请问有人可以告诉我如何解决这个问题吗?下面是我代码的截图。
英文:
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.<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)
can some please let me know how to do that. below i have attached screenshot of my code.
答案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("notoserifdevanagaribold.ttf")) {
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:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论