Cargar una fuente desde un archivo TTF | .NETO

A continuación se describen cuatro ejemplos realizados de forma diferente sobre cómo cargar una fuente desde el archivo Montserrat-Regular.ttf.


Primero aprenda los fundamentos de carga en la página ¿Cómo cargar fuentes?.

Agregue los siguientes espacios de nombres al principio del archivo:

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

Cargando desde el archivo usando el objeto FileSystemStreamSource

Realice los siguientes pasos para cumplir con la operación:

  1. Construya la ruta al archivo.
  2. Inicie el objeto FontDefiniton.
  3. Establezca fileExtension en ttf.
  4. Cargue la fuente.
 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);

Cargando desde el archivo usando el objeto FileInfo

Para completar la carga haga lo siguiente:

  1. Construya la ruta al archivo.
  2. Inicie el objeto FontDefiniton pasando TTF como valor FontType.
  3. Obtenga el valor calculado automáticamente fileExtension.
  4. Cargue la fuente.
 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);

Cargando desde el archivo excluyendo el objeto FontFileDefinition de la cadena de inicialización

Se deben realizar las siguientes acciones para cargar la fuente de esta manera:

  1. Construya la ruta al archivo.
  2. Inicie el objeto FontDefiniton pasando TTF como valor FontType, ttf como valor fileExtension y objeto FileSystemStreamSource . El parámetro fileExtension aquí no es un valor duplicado para el parámetro FontType.
  3. Cargue la fuente.
 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);     

Cargando la fuente desde la matriz de bytes

Para cargar la fuente desde la matriz de bytes:

  1. Construya la ruta al archivo.
  2. Cargue datos binarios de fuente en la matriz de bytes.
  3. Inicialice el objeto FontDefiniton pasando TTF como valor FontType, ttf como valor fileExtension y ByteContentStreamSource objeto basado en la matriz fontBytes.
  4. Cargue la fuente.
 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);

Puede obtener ejemplos sobre cómo usar Aspose.Font en la solución Aspose.Font.Examples.sln, en los net-examples de la Documentación de Aspose.Font.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.