Cargar una fuente desde un archivo CFF | .NETO

En esta página veremos ejemplos de carga de la fuente CenturyGothic colocada en el archivo CenturyGothic.cff.


Si no leyó los fundamentos de carga de Aspose.Font, vaya a ¿Cómo cargar fuentes? página.

Primero debe notificar 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 FileInfo

Siga el algoritmo para completar la carga de fuentes:

  1. Construya la ruta al archivo.
  2. Inicie el objeto FontDefiniton pasando CFF 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, "CenturyGothic.cff");
 3
 4    // Initialize FontDefinition object passing CFF 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.CFF, fileDef);
 9
10    // Load the font 
11    Font font = Font.Open(fontDef);

Carga de fuentes con la variable de tipo byte[] y con el uso del objeto de tipo ByteContentStreamSource

Para cargar la fuente de esta manera, debe seguir los siguientes pasos:

  1. Construya la ruta al archivo.
  2. Inicialice el objeto FontDefiniton pasando CFF como valor FontType, cff como valor fileExtension y ByteContentStreamSource objeto basado en la matriz fontBytes.
  3. Cargue la fuente.
 1    // Construct path to the file
 2    string fontPath = Path.Combine(DataDir, "CenturyGothic.cff");
 3
 4    // Load font binary data into byte array
 5    byte[] fontBytes = File.ReadAllBytes(fontPath);
 6
 7    // Initialize FontDefinition object  passing CFF as FontType value, "cff" as fileExtension value, 
 8    // and ByteContentStreamSource object based on fontBytes array
 9    FontDefinition fontDef = new FontDefinition(FontType.CFF, "ttf", new ByteContentStreamSource(fontBytes));
10
11    // Load the font
12    Font font = Font.Open(fontDef);

Hay más 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.