1

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.

int FONT = R.font.notoserifdevanagaribold;

Ajinkya kadam
  • 542
  • 1
  • 6
  • 16
  • ***public static PdfFont createFont(String fontProgram, String encoding)*** – Ajinkya kadam Jan 03 '20 at 08:31
  • i have to pass file name like this ```PdfFont f = PdfFontFactory.createFont("notoserifdevanagaribold.ttf", PdfEncodings.IDENTITY_H);``` – Ajinkya kadam Jan 03 '20 at 08:39
  • i am getting this error ```W/System.err: com.itextpdf.io.IOException: Font file notoserifdevanagaribold.ttf not found``` – Ajinkya kadam Jan 03 '20 at 08:42
  • (https://stackoverflow.com/q/19225857/3409734) _Im trying to use a custom font, and I've read that I suppose to place the fonts in assets/fonts._. From this post I guess you need to place your fonts folder in asset folder and not in resource folder – SimplyProgrammer Jan 03 '20 at 09:02
  • i have deleted that font folder from resource folder now i have created in assets folder but then also i am getting error ```W/System.err: com.itextpdf.io.IOException: Font file fonts/notoserifdevanagaribold.ttf not found.``` here i have to access the font path as a string in ***PdfFont f = PdfFontFactory.createFont("fonts/notoserifdevanagaribold.ttf", PdfEncodings.IDENTITY_H);*** in this method – Ajinkya kadam Jan 03 '20 at 09:06
  • try adding `"assets/fonts/notoserifdevanagaribold.ttf"` instead of `"notoserifdevanagaribold.ttf"` – SimplyProgrammer Jan 03 '20 at 09:53
  • still getting error ```W/System.err: com.itextpdf.io.IOException: Font file assets/fonts/notoserifdevanagaribold.ttf not found.``` – Ajinkya kadam Jan 03 '20 at 09:55
  • follow this answer on how to pull resources in Android https://stackoverflow.com/a/45908819/1566339 and then convert it to byte[] https://stackoverflow.com/a/15098868/1566339 (createFont also takes a byte[] as the first argument). I am very rusty in Android programming, but it feels like this should work. – André Lemos Jan 03 '20 at 12:52
  • ***i have changed to*** ```AssetManager am = this.getAssets(); InputStream is = am.open("notoserifdevanagaribold.ttf"); byte[] music = new byte[is.available()]; PdfFont f = PdfFontFactory.createFont(music, PdfEncodings.IDENTITY_H);``` ***but i am getting error on PdfFont line*** ```W/System.err: com.itextpdf.io.IOException: Type of font is not recognized``` – Ajinkya kadam Jan 03 '20 at 13:36

1 Answers1

2

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>
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60