Skip to content
ComPDF
Guides

Font Management

The ComPDF SDK supports reading font families and their styles that are already installed on the device and applying them to annotations, forms, watermarks, headers and footers, Bates numbering, and other features. You can also import custom fonts to create more visually appealing PDF documents and meet various business requirements.

Import Fonts

By default, the SDK uses the system fonts available on the device. Since font sets may vary by region or device, you can extend the available font styles by importing custom fonts.

Follow these steps to import fonts:

  1. Place the font files in a specified directory.
  2. Call ComPDFKit.setImportFontDir(dir, addSysFont) to set the font directory.
  3. Initialize the SDK.
tsx
// Copy fonts from the assets directory to local storage
const fontDir = await CPDFFileUtil.copyAssetsFolderToStorage('extraFonts');

// Whether to include system fonts in the returned font list
bool addSysFont = true;

// Set the custom font directory
await ComPDFKit.setImportFontDir(fontDir, addSysFont);

// Initialize the SDK
await ComPDFKit.initWithPath(
  Platform.OS == "android"
    ? "assets://license_key_rn.xml"
    : "license_key_rn.xml"
);

Note: The font import API must be called before initializing the SDK.

Updating Fonts

After the SDK has been initialized, you can use the ComPDFKit.updateImportFontDir(fontDir, addSysFont: false) API to refresh the font list and ensure that newly imported fonts are recognized and available.

tsx
// Copy fonts from the assets directory to local storage
const fontDir = await CPDFFileUtil.copyAssetsFolderToStorage('extraFonts2');

// Update the font list
const result = await ComPDFKit.updateImportFontDir(fontDir, false);

Retrieving Fonts

When creating annotations, forms, watermarks, or other objects, you can retrieve the list of available fonts using ComPDFKit.getFonts().

tsx
const fonts = await ComPDFKit.getFonts();
for (const font of fonts) {
  const familyName = font.familyName;
  const styleName = font.styleNames[0];
}

About Font Families and Style Names

  1. Font Family:

    A font family refers to a group of fonts that share the same design style. For example, the Helvetica font family includes multiple styles such as Helvetica Regular, Helvetica Bold, and Helvetica Italic, all of which belong to the same font family.

  2. Font Style:

    A font style refers to a specific variant within a font family that distinguishes different appearances such as regular, bold, italic, or other variations. Using the Helvetica font family as an example, Regular, Bold, and Italic are different style names within the same family.