Font Management
ComPDFKit PDF SDK supports reading the existing font families and their styles on your device, and setting them as fonts for annotations, forms, watermarks, headers, footers, bates stamps, and many other functionalities. This will help you design aesthetically pleasing PDF documents or adjust and refine your PDF documents with fonts that comply with certain specifications.
Get font family
When setting fonts using font management, you need to:
- Retrieve all font family names available in the system.
- Choose the font you need and obtain the
CPDFFontName
object representing the font. - Retrieve the collection of styles from the
CPDFFontName
object. - After selecting a style name, obtain the
PsName
based on the font family name and style name. - The
PsName
object can then be used for setting the font.
Below are the code examples:
// Retrieve all font family names available in the system
List<CPDFFontName> list = CPDFFont.getOutFontName();
// Choose the font and obtain the CPDFFontName object
CPDFFontName cpdfFontName = list.get(index);
// Retrieve the collection of styles
List<String> fontStyles = cpdfFontName.getStyleName();
// Obtain the `PsName` object based on the font family and font style
String psName = CPDFTextAttribute.FontNameHelper.obtainFontName(cpdfFontName.getFamilyName(), styleName);
// Apply the `psName` object as the font name for the desired feature. For specific attribute settings, refer to the documentation of corresponding features.
// Example: Set font for FreeText annotation
CPDFFreetextAnnotation freetextAnnotation = freetextAnnot.onGetAnnotation();
freetextAnnotation.setFreetextDa(new CPDFTextAttribute(psName, fontSize, fontColor));
// Example: Set font for Form ListBox widget
CPDFListboxWidget listBoxWidget = (CPDFListboxWidget) baseAnnotImpl;
listBoxWidget.setFontName(psName);
// Retrieve all font family names available in the system
val list : List<CPDFFontName> = CPDFFont.getOutFontName()
// Choose the font and obtain the CPDFFontName object
val cpdfFontName : CPDFFontName = list.get(index)
// Retrieve the collection of styles
val fontStyles : List<String> = cpdfFontName.getStyleName()
// Obtain the `PsName` object based on the font family and font style
val psName : String = CPDFTextAttribute.FontNameHelper.obtainFontName(cpdfFontName.getFamilyName(), styleName)
// Apply the `psName` object as the font name for the desired feature. For specific attribute settings, refer to the documentation of corresponding features.
// Example: Set font for FreeText annotation
val freetextAnnotation = freetextAnnot.onGetAnnotation()
freetextAnnotation.setFreetextDa(CPDFTextAttribute(psName, fontSize, fontColor))
// Example: Set font for Form ListBox widget
val listBoxWidget = baseAnnotImpl as CPDFListboxWidget
listBoxWidget.setFontName(psName)
About Font Family and Style Names
Font Family:
Font Family refers to the group or series name of a font, typically representing a set of fonts that share a common design style.
For example, the Helvetica font family includes various styles such as Helvetica Regular, Helvetica Bold, and Helvetica Italic, all of which belong to the Helvetica font family.
Font Style:
Font Style refers to the specific style or variant name of a font. It is commonly used to differentiate between different font 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.
Import Font
The fonts currently in use are obtained from the device's system, and the font collections available may vary across devices in different regions. You can enrich the available font styles by using the font import interface.
To import fonts, follow the steps below:
- Copy the fonts you need to a designated folder path.
- Use
CPDFSdk.setImportFontDir()
to set the directory where your fonts are stored. - Initialize the SDK.
// Copy fonts from the assets directory to storage
String fontDir = new File(getFilesDir(), "extraFonts/").getAbsolutePath();
CFileUtils.copyAssetsDirToPhone(this, "extraFonts", getFilesDir().getAbsolutePath());
// Determine whether to include system fonts, which affects whether system fonts are included in CPDFFont.getOutFontName();
boolean addSysFont = true;
// Set the specified font folder
CPDFSdk.setImportFontDir(savePath, addSysFont);
// Initialize the SDK
CPDFSdk.init(this, "your license key", true);
// Copy fonts from the assets directory to storage
val fontDir = File(filesDir, "extraFonts/").absolutePath
CFileUtils.copyAssetsDirToPhone(this, "extraFonts", filesDir.absolutePath)
// Determine whether to include system fonts, which affects whether system fonts are included in CPDFFont.getOutFontName();
val addSysFont = true
// Set the specified font folder
CPDFSdk.setImportFontDir(savePath, addSysFont)
// Initialize the SDK
CPDFSdk.init(this, "your license key", true)
Note: The font import interface must be configured before initializing the SDK. Please ensure that you remove the SDK initialization configuration in the
AndroidManifest.xml
and instead initialize the SDK programmatically.