Skip to content

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.

When setting fonts using font management, you need to:

  1. Retrieve all font family names available in the system.
  2. Choose the font you need and obtain the CPDFFontName object representing the font.
  3. Retrieve the collection of styles from the CPDFFontName object.
  4. After selecting a style name, obtain the PsName based on the font family name and style name.
  5. The PsName object can then be used for setting the font.

Below are the code examples:

java
// 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);
kotlin
// 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

  1. 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.

  2. 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.