Customize PDF fonts for Android applications
PDF files should render consistently across different PDF viewers. This consistency is possible because a PDF file can embed the fonts required for rendering.
However, in some cases — due to file size or other considerations — PDFs don’t embed fonts. When this happens, the PDF viewer relies on system fonts, which may cause rendering issues if the required fonts are unavailable.
Embedding fonts in PDFs is the best way to ensure accurate rendering, but this isn’t always possible, especially when working with third-party PDFs. Custom font path support addresses this issue.
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, which are outlined below.
-
Disabling automatic initialization of Nutrient by omitting the
nutrient_license_key
metadata element in your Android manifest. This is necessary, since you need to provide custom font paths when initializing Nutrient manually in the next step. -
Initializing Nutrient manually and providing the paths to your custom fonts:
// 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. Nutrient.initialize(this, licenseString, fontFolders)
// 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. Nutrient.initialize(this, licenseString, fontFolders);
If you’re 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 your app, and all .ttf
, .ttc
, and .otf
files will be added to the font list of Nutrient.
Microsoft core fonts
Microsoft core fonts are widely used on the web and in PDFs. Adding them as custom fonts improves document conversion and rendering accuracy. Nutrient doesn’t include these fonts because Microsoft no longer provides them directly, and redistribution is prohibited by license. To use these fonts, download them from SourceForge and add them as custom fonts.