Charger une police à partir d'un fichier TTF | .FILET

Voici quatre exemples réalisés différemment sur la façon de charger une police à partir du fichier Montserrat-Regular.ttf.


Apprenez d’abord les principes fondamentaux du chargement sur la page Comment charger les polices ?.

Ajoutez les espaces de noms suivants en tête du fichier :

1using System;
2using Aspose.Font;
3using Aspose.Font.Sources;
4using System.IO;

Chargement à partir du fichier à l’aide de l’objet FileSystemStreamSource

Suivez les étapes suivantes pour réaliser l’opération :

  1. Construisez le chemin d’accès au fichier.
  2. Initiez l’objet FontDefiniton.
  3. Définissez fileExtension sur ttf.
  4. Chargez la police.
 1    // Construct path to the file
 2    string fontPath = Path.Combine(DataDir, "Montserrat-Regular.ttf");
 3
 4    // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
 5    FontFileDefinition fileDef = new FontFileDefinition("ttf", new FileSystemStreamSource(fontPath));
 6
 7    // Based on FileSystemStreamSource object, set fileExtension to "ttf"
 8    FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
 9
10    // Load the font
11    Font font = Font.Open(fontDef);

Chargement à partir du fichier à l’aide de l’objet FileInfo

Pour effectuer le chargement, procédez comme suit :

  1. Construisez le chemin d’accès au fichier.
  2. Initiez l’objet FontDefiniton en passant TTF comme valeur FontType.
  3. Obtenez la valeur calculée automatiquement fileExtension.
  4. Chargez la police.
 1     // Construct path to the file
 2     string fontPath = Path.Combine(DataDir, "Montserrat-Regular.ttf");
 3
 4     // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
 5     FontFileDefinition fileDef = new FontFileDefinition(new FileInfo(fontPath));
 6
 7     // Based on FileInfo object, fileExtension value is calculated automatically from FileInfo fields.
 8     FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
 9
10     // Load the font
11     Font font = Font.Open(fontDef);

Chargement à partir du fichier excluant l’objet FontFileDefinition de la chaîne d’initialisation

Les actions suivantes doivent être entreprises pour charger la police de cette façon :

  1. Construisez le chemin d’accès au fichier.
  2. Initiez l’objet FontDefiniton en passant TTF comme valeur FontType, ttf comme valeur fileExtension et l’objet FileSystemStreamSource . Le paramètre fileExtension n’est pas ici une valeur en double pour le paramètre FontType.
  3. Chargez la police.
 1     // Construct path to the file
 2     string fontPath = Path.Combine(DataDir, "Montserrat-Regular.ttf");
 3
 4     // Initialize FontDefinition object passing TTF as FontType value, "ttf" as fileExtension value, 
 5     // and FileSystemStreamSource object. Parameter 'fileExtension' here is not duplicate value 
 6     // for parameter 'FontType' and it's needed for correct font format detection
 7     FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new FileSystemStreamSource(fontPath));
 8
 9     // Load the font
10     Font font = Font.Open(fontDef);     

Chargement de la police à partir du tableau d’octets

Pour charger la police à partir du tableau d’octets :

  1. Construisez le chemin d’accès au fichier.
  2. Chargez les données binaires de la police dans le tableau d’octets.
  3. Initialisez l’objet FontDefiniton en passant TTF comme valeur FontType, ttf comme valeur fileExtension et ByteContentStreamSource objet basé sur le tableau fontBytes.
  4. Chargez la police.
 1     // Construct path to the file
 2     string fontPath = Path.Combine(DataDir, "Montserrat-Regular.ttf");
 3
 4     // Load font binary data into byte array
 5     byte[] fontBytes = File.ReadAllBytes(fontPath);
 6
 7     // Initialize FontDefinition object  passing TTF as FontType value, "ttf" as fileExtension value, 
 8     // and ByteContentStreamSource object based on fontBytes array
 9     FontDefinition fontDef = new FontDefinition(FontType.TTF), "ttf", new ByteContentStreamSource(fontBytes);
10
11     // Load the font
12     Font font = Font.Open(fontDef);

Vous pouvez obtenir des exemples sur la façon d’utiliser Aspose.Font dans la solution Aspose.Font.Examples.sln, dans les net-examples de la documentation Aspose.Font.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.