从TTF文件加载字体| .NET
Contents
[
Hide
Show
]此处描述了四个不同满足的示例,介绍了如何从文件蒙特塞拉特regular.ttf中加载字体。
首先在 如何加载字体上学习加载基础知识?页。
在文件的头部添加下一个名称空间:
1 using System;
2 using Aspose.Font;
3 using Aspose.Font.Sources;
4 using System.IO;
使用filesystemstreamsource对象从文件加载
采取下一步完成操作:
- 构建文件的路径。
- 启动 fontdefiniton对象。
- 将
fileextension设置为
ttf
。 - 加载字体。
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);
使用FileInfo对象从文件加载
要实现加载,请执行下一个:
- 构建文件的路径。
- 启动
fontdefiniton对象传递
ttf
为 fonttype值。 - 获取自动计算的值 fileextension。
- 加载字体。
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);
从文件中加载不包括初始化链中的fontfiledefinition对象
必须采取下一个操作以通过这种方式加载字体:
- 构建文件的路径。
- 启动
fontdefiniton对象传递
ttf
作为 fonttypevalue,ttf
as fileextension值(9)值和 FilesystemStreamSource对象。参数 fileextension这不是参数 fonttype的重复值。 - 加载字体。
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);
从字节数组加载字体
从字节数组加载字体:
- 构建文件的路径。
- 将字体二进制数据加载到字节数组中。
- 初始化
fontdefiniton对象将
ttf
作为 fonttypevalue,ttf
as fileextension值(15)和 bytecontentstreamsource基于Fontbytes array的对象。 - 加载字体。
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);
您可以在 aspose.font.examples.sln解决方案中,在 net-examples的 aspose.font document.font document.中获取有关如何使用aspose.font的示例。