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:
- Retrieve all font family names available in the system.
- Choose the font you need and obtain the style names of the font family.
- After selecting the style name, obtain the
CPDFFont
object based on the font family name and style name. - The
CPDFFont
object can then be used to set the font. - Set the application-side font UIFont through the
CPDFFont
object.
Here is a code example:
// 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"
// 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
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
CPDFFont.setImportDir(path, isContainSysFont: true)
to set the directory where your fonts are stored. - Initialize the SDK.
// Copy fonts from the Bundle directory to sandbox.
let dir = Bundle.main.path(forResource: "Font", ofType: nil) ?? ""
let fileManager = FileManager.default
let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationPath = documentDirectory.appendingPathComponent("Font")
do {
if fileManager.fileExists(atPath: destinationPath.path) {
try fileManager.removeItem(at: destinationPath)
}
try fileManager.copyItem(atPath: dir, toPath: destinationPath.path)
// Set the specified font folder
// isContainSysFont determine whether to include system fonts
CPDFFont.setImportDir(destinationPath.path, isContainSysFont: true)
// Initialize the SDK
CPDFKit.verify(withKey: "Your license key")
} catch {
print("Error copying Font directory: \(error)")
}
// Copy fonts from the Bundle directory to sandbox.
NSString *dir = [[NSBundle mainBundle] pathForResource:@"Font" ofType:nil] ?: @"";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *destinationPath = [documentDirectory URLByAppendingPathComponent:@"Font"];
NSError *error = nil;
if ([fileManager fileExistsAtPath:[destinationPath path]]) {
if (![fileManager removeItemAtURL:destinationPath error:&error]) {
NSLog(@"Error removing existing directory: %@", error);
}
}
if (![fileManager copyItemAtPath:dir toPath:[destinationPath path] error:&error]) {
NSLog(@"Error copying Font directory: %@", error);
} else {
// Set the specified font folder
// isContainSysFont determine whether to include system fonts
[CPDFFont setImportFontDir:[destinationPath path] isContainSysFont:YES];
// Initialize the SDK
[CPDFKit verifyWithKey:@"Your license key"];
}