Skip to content
Guides

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 style names of the font family.
  3. After selecting the style name, obtain the CPDFFont object based on the font family name and style name.
  4. The CPDFFont object can then be used to set the font.
  5. Set the application-side font UIFont through the CPDFFont object.

Here is a code example:

swift
// Retrieve all font family names available in the system
let fonts: [String] = CPDFFont.familyNames

// Obtain the list of font styles corresponding to the font family name and choose a font style
let fontStyles = CPDFFont.fontNames(forFamilyName: "familyName")

// Obtain the CPDFFont object based on the font family and font style
let font =  CPDFFont(familyName: "familyName", fontStyle: "fontStyle")

// Apply the name of the CPDFFont object to the functionality that needs to set the font.
// For specific setting property methods, refer to the documentation of the corresponding functionality.
let anotation = CPDFLineAnnotation(document: document)
anotation.cFont = font

// Set the UIFont on the application side using the CPDFFont object
let appleFont = UIFont.init(name: CPDFFont.convertAppleFont(cFont ?? CPDFFont(familyName: "Helvetica", fontStyle: "")) ?? "Helvetica"
objective-c
// Retrieve all font family names available in the system
NSArray *fonts = [CPDFFont familyNames];

// Obtain the list of font styles corresponding to the font family name and choose a font style
NSArray *fontStyles = [CPDFFont fontNamesForFamilyName:@"familyName"];

// Obtain the CPDFFont object based on the font family and font style
CPDFFont *font = [[CPDFFont alloc] initWithFamilyName:@"familyName" fontStyle:@"fontStyle"];

// Apply the name of the CPDFFont object to the functionality that needs to set the font.
// For specific setting property methods, refer to the documentation of the corresponding functionality.
CPDFLineAnnotation *anotation = [[CPDFLineAnnotation alloc] initWithDocument:document];
[anotation setCFont:font];
 
// Set the UIFont on the application side using the CPDFFont object
 NSString *fontName = [CPDFFont convertAppleFont:cFont];
 UIFont *appleFont = [UIFont fontWithName:fontName size:18];

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.