Custom Font Path

PDF files are supposed to be rendered exactly the same no matter what PDF viewer you’re using. One reason why this is the case is because a PDF file can embed the fonts required to render it.

Sometimes, due to size reasons or other considerations, the fonts are not embedded, which makes the PDF viewer look for the font on the current system. Depending on what fonts are available, this can cause rendering problems.

While the best option is to always embed the fonts in the PDF, this isn’t always possible, especially if you’re working with third-party PDF files. This is where custom font path support comes in.

There are many varieties of Android devices out there, and not all of them come with a full set of fonts. For example, some of them are missing some international fonts, so you may have to ship them with your application.

The Android SDK download comes with an example of how to properly use custom font paths. This can be found in examples/custom-fonts.

Loading Custom Fonts

Using custom fonts in your app requires two steps:

  1. Disabling automatic initialization of PSPDFKit by omitting the pspdfkit_license_key metadata element in your Android manifest. This is necessary, since you need to provide custom font paths when initializing PSPDFKit manually in the next step.

  2. Initializing PSPDFKit manually, providing the paths to your custom fonts:

    Copy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    // You can use any folder (private or on the external storage) for
    // fonts. It is important that your font files are inside this folder
    // and that your app has read access to it.
    val appFonts = getDir("fonts", Context.MODE_PRIVATE)
    
    // You can provide as many additional custom font folders as you like.
    val fontFolders = listOf(appFonts.absolutePath)
    
    // Custom font folders are provided at library initialization time.
    PSPDFKit.initialize(this, licenseString, fontFolders)
    
    Copy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    // You can use any folder (private or on the external storage) for
    // fonts. It is important that your font files are inside this folder
    // and that your app has read access to it.
    File appFonts = getDir("fonts", Context.MODE_PRIVATE);
    
    // You can provide as many additional custom font folders as you like.
    List<String> fontFolders = Arrays.asList(appFonts.absolutePath);
    
    // Custom font folders are provided at library initialization time.
    PSPDFKit.initialize(this, licenseString, fontFolders);
    

If you are missing international fonts when rendering on certain Android devices, the Google Noto Fonts are usually a good choice to include in your app.

The font directory can be any directory that is accessible to the app, and all .ttf, .ttc, and .otf files will be added to the font list of PSPDFKit.