从CFF文件加载字体| .NET
Contents
[
Hide
Show
]在此页面上,我们将查看file centurygothic.cff中加载字体centurygothic
的示例。
如果您没有阅读Aspose.font加载基本面,请转到 如何加载字体?页。
首先,您需要通知文件头部的下一个名称空间:
1 using System;
2 using Aspose.Font
3 using Aspose.Font.Sources;
4 using System.IO;
使用FileInfo对象从文件加载
遵循该算法来实现字体加载:
- 构建文件的路径。
- 启动
fontdefiniton对象传递
cff
为 fonttype值。 - 获取自动计算的值 fileextension。
- 加载字体。
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);
字体加载字节[]类型变量以及使用ByteContentsTreamSource类型对象
要通过这种方式加载字体,您需要采取以下步骤:
- 构建文件的路径。
- 初始化
fontdefiniton对象将
cff
作为 fonttype值,cff
as fileextension值(7)和 bytecontentstreamsource基于fontbytes array array array array上。 - 加载字体。
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);
有关如何使用aspose.font的更多示例在 aspose.font.examples.sln解决方案中,在 aspose.font document.font document.的 net-examples中。