Font Management
ComPDF SDK supports reading font families and their styles from your device and applying them to annotations, forms, watermarks, headers, footers, Bates codes, and other features. This helps you design more visually appealing PDF files or import custom fonts to meet specific requirements.
Import Fonts
By default, the SDK uses fonts from the device system, and devices in different regions may contain different font sets. You can expand the available font styles by importing fonts through the font import interface.
To import fonts, follow these steps:
- Place the font files in a specified folder path.
- Use
ComPDFKit.setImportFontDir(dir, addSysFont)to set the font directory. - Initialize the SDK.
// Copy fonts from the assets directory to storage
final fontDir = await extractAssetFolder(context, 'extraFonts/');
// Determine whether to add system fonts, this affects whether system fonts are included in ComPDFKit.getFonts() results
bool addSysFont = true;
// Set the specified font folder
await ComPDFKit.setImportFontDir(fontDir, addSysFont: addSysFont);
// Initialize the SDK
await ComPDFKit.initWithPath(licenseFile.path);Note: The font import interface must be called before initializing the SDK.
Update Fonts
After initializing the SDK, you can use the ComPDFKit.updateImportFontDir(fontDir, addSysFont: false) interface to refresh the font list, ensuring that newly imported fonts can be properly recognized and used.
// Copy fonts from the assets directory to storage
final fontDir = await extractAssetFolder(context, 'extraFonts2/');
// Update the font list
final result = await ComPDFKit.updateImportFontDir(fontDir, addSysFont: false);Get Fonts
When creating annotations, forms, watermarks, and other objects, you can use ComPDFKit.getFonts() to retrieve the list of currently available fonts.
List<CPDFFontName> fonts = await ComPDFKit.getFonts();
for (var font in fonts) {
final familyName = font.familyName;
final styleName = font.styleNames[0];
}About Font Family and Style Names
Font Family:
A font family refers to a series of fonts that share the same design style. For example, the Helvetica font family contains multiple styles such as Helvetica Regular, Helvetica Bold, Helvetica Italic, etc., all belonging to the Helvetica font family.
Font Style:
Font style refers to specific variants within a font family, used to distinguish different styles within the same font family, such as bold, italic, regular, etc. Taking the Helvetica font family as an example, Regular, Bold, Italic, etc., are all different style names.