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.
Font availability varies across Android devices. Bundle missing fonts with your app when documents require them, such as fonts for additional languages.
The custom fonts example(opens in a new tab) on GitHub demonstrates custom font paths.
Loading custom fonts
Custom font paths require manual initialization. Follow these steps:
- Set
nutrient_automatic_initializetofalsein your Android manifest to disable automatic initialization. - Initialize Nutrient manually with your custom font paths.
The following example passes a font directory during initialization:
// 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, InitializationOptions(licenseKey = licenseString, fontPaths = 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.getAbsolutePath());
// Custom font folders are provided at library initialization time. Nutrient.initialize(this, new InitializationOptions(licenseString, fontFolders, null, null));Consider Google Noto Fonts(opens in a new tab) for languages that a device’s fonts don’t support.
Use any directory your app can read, and Nutrient will add all .ttf, .ttc, and .otf files from that directory to its font list.
Microsoft core fonts
Microsoft core fonts are widely used on the web and in PDFs. Adding them as custom fonts improves rendering accuracy. Nutrient doesn’t include these fonts because Microsoft no longer provides them directly, and redistribution is prohibited by license(opens in a new tab). To use these fonts, download them from SourceForge(opens in a new tab) and add them as custom fonts.