Skip to content
ComPDF
Guides

Font Management

The ComPDF SDK supports reading font families and their styles installed on the device and applying them to annotations, forms, watermarks, headers & footers, Bates numbers, and more. With font management capabilities, you can design the visual appearance of PDFs more flexibly or adjust and enhance PDF documents using fonts that meet specific specification requirements.


Obtaining Font Families and Styles

Before setting a font, the following steps are typically required:

  1. Retrieve all available font families (Font Family) on the system.
  2. Select a target font family from the list and obtain the corresponding CPDFFontName object.
  3. From CPDFFontName, retrieve the list of supported styles (Font Style) for that family.
  4. Generate the corresponding font PsName based on the font family name and style name.
  5. Use the generated PsName to set fonts for annotations, forms, and other features.

Example code:

java
// Get all font families available on the system
List<CPDFFontName> fontList = CPDFFont.getFontName();

// Select a specific font family
CPDFFontName fontName = fontList.get(index);

// Get the list of styles supported by this font family
List<String> fontStyles = fontName.getStyleName();

// Generate PsName based on font family name and style name
String psName = CPDFTextAttribute.FontNameHelper.obtainFontName(
        fontName.getFamilyName(),
        fontStyles.get(0)
);

// Example 1: Set FreeText annotation font
CPDFFreetextAnnotation freetextAnnotation = freetextAnnot.onGetAnnotation();
freetextAnnotation.setFreetextDa(
        new CPDFTextAttribute(psName, fontSize, fontColor)
);

// Example 2: Set ListBox form font
CPDFListboxWidget listBoxWidget = (CPDFListboxWidget) baseAnnotImpl;
listBoxWidget.setFontName(psName);
kotlin
// Get all font families available on the system
val fontList: List<CPDFFontName> = CPDFFont.getFontName()

// Select a specific font family
val fontName: CPDFFontName = fontList[index]

// Get the list of styles supported by this font family
val fontStyles: List<String> = fontName.styleName

// Generate PsName based on font family name and style name
val psName: String = CPDFTextAttribute.FontNameHelper.obtainFontName(
    fontName.familyName,
    fontStyles[0]
)

// Example 1: Set FreeText annotation font
val freetextAnnotation = freetextAnnot.onGetAnnotation()
freetextAnnotation.setFreetextDa(
    CPDFTextAttribute(psName, fontSize, fontColor)
)

// Example 2: Set ListBox form font
val listBoxWidget = baseAnnotImpl as CPDFListboxWidget
listBoxWidget.setFontName(psName)

Font Family and Font Style Description

Font Family

A font family is the series name of a font and represents a collection of fonts with a unified design style.

For example, Helvetica is a font family that includes multiple styles, such as:

  • Helvetica Regular
  • Helvetica Bold
  • Helvetica Italic

These fonts share a consistent visual style but differ in weight or italicization.

Font Style

A font style is used to distinguish specific variants within the same font family, such as regular, bold, or italic.

For the Helvetica font family, Regular, Bold, and Italic are different styles under the same family.


Importing Custom Fonts

By default, the SDK uses fonts installed on the device system. Since system font sets may vary across regions and devices, you can extend the available fonts by importing custom fonts.

Steps:

  1. Copy the required font files (such as .ttf or .otf) to a specified directory.
  2. Use CPDFSdk.setImportFontDir() to set the font directory.
  3. Complete the font directory configuration before initializing the SDK.
java
// Copy fonts from assets to the app storage directory
String fontDir = new File(getFilesDir(), "extraFonts/").getAbsolutePath();
CFileUtils.copyAssetsDirToPhone(
        this,
        "extraFonts",
        getFilesDir().getAbsolutePath()
);

// Whether to also load system fonts
boolean addSysFont = true;

// Set font import directory
CPDFSdk.setImportFontDir(fontDir, addSysFont);

// Initialize SDK
CPDFSdk.init(this, "your license key", true);
kotlin
// Copy fonts from assets to the app storage directory
val fontDir = File(filesDir, "extraFonts/").absolutePath
CFileUtils.copyAssetsDirToPhone(
    this,
    "extraFonts",
    filesDir.absolutePath
)

// Whether to also load system fonts
val addSysFont = true

// Set font import directory
CPDFSdk.setImportFontDir(fontDir, addSysFont)

// Initialize SDK
CPDFSdk.init(this, "your license key", true)

Note The font import directory must be set before SDK initialization. Please remove the SDK auto-initialization configuration from AndroidManifest.xml and initialize the SDK manually via code.

Updating the Imported Font Directory

After the SDK has been initialized, you can still dynamically update the currently used font directory via CPDFDocument.setImportFontDir().

java
String fontDir = new File(getFilesDir(), "extraFonts2/").getAbsolutePath();
CFileUtils.copyAssetsDirToPhone(
        this,
        "extraFonts2",
        getFilesDir().getAbsolutePath()
);

boolean addSysFont = true;

// Update font directory
CPDFDocument.setImportFontDir(fontDir, addSysFont);
kotlin
val fontDir = File(filesDir, "extraFonts2/").absolutePath
CFileUtils.copyAssetsDirToPhone(
    this,
    "extraFonts2",
    filesDir.absolutePath
)

val addSysFont = true

// Update font directory
CPDFDocument.setImportFontDir(fontDir, addSysFont)

Note After re-specifying the font directory, any previously set font directories will be cleared, and only the fonts in the currently specified directory will be retained.

After the update, you can retrieve the latest available font list via CPDFFont.getFontName().