Skip to content

Font Management

The ComPDFKit PDF SDK supports reading the font families and their styles available on your device, and setting them as fonts for various functionalities such as annotations, forms, watermarks, headers, footers, Bates numbering, and more. This will assist you in designing aesthetically pleasing PDF files or adjusting and enhancing your PDF files with fonts that comply with certain specifications.

When setting fonts using font management, you need to:

  1. Call CPDFFont.InitFont(); to initialize the font library. This function only needs to be called once during the project's lifecycle.
  2. Retrieve the names of all font families in the system.
  3. Select the font you need and obtain the style names for the font family.
  4. After selecting the style name, obtain the PostScript name of the font based on the font family name and style name.
  5. The PostScript name can then be used to set the font.

This example shows how to use font management:

C#
// Initialize the font library
CPDFFont.InitFont();

int familyNameIndex = 0;
int styleNameIndex = 0;

// Get the list of font families and choose a font family
List<string> fontFamilyNames = CPDFFont.GetFontNameDictionary().Keys.ToList();
string fontFamilyName = fontFamilyNames[familyNameIndex];

// Get the list of font styles corresponding to the font family and choose a font style
List<string> fontStyleNames = CPDFFont.GetFontNameDictionary()[fontFamilyName];
string fontStyleName = fontStyleNames[styleNameIndex];

// Get the PostScript name based on the font family and font style
string postScriptName = string.Empty;
CPDFFont.GetPostScriptName(fontFamilyName, fontStyleName, ref postScriptName);

...
// Apply the PostScript name to the functionality that requires font setting. 
// For specific attribute settings, refer to the documentation of the corresponding functionality.
CPDFWatermark watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
watermark.SetFontName(postScriptName);

About Font Family, Font Style, and PostScript Name

  1. Font Family:

    Font Family refers to the group or series name of a font, typically representing a collection of fonts sharing a similar design style.

    For example, the Helvetica font family comprises various styles of fonts such as Helvetica Regular, Helvetica Bold, Helvetica Italic, etc., all belonging 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.

  3. PostScript Name:

    The PostScript name is the unique identifier for a font. It is typically a distinct string used to specify a unique combination of font family and style.

    It serves as a standardized name for fonts, allowing them to be accurately referenced across different systems and platforms.