正在加载 Type-1 (PostScript) 字体 | .NET
Contents
[
Hide
Show
]使用 Aspose.Font for .NET 加载 Type‑1 字体
为什么使用 Type‑1 字体?
- Type‑1(PostScript)字体 – .pfb(二进制)和 .pfa(ASCII) – 仍在许多传统出版和 CAD 工作流中使用。
- Aspose.Font 能够读取、分析、渲染和转换这些字体,无需任何外部依赖,非常适合服务器端处理和文档生成。
所需条件
| 项目 | 最低版本 |
|---|---|
| .NET Framework | 4.6(或 .NET Core 2.x / .NET 5+) |
| Aspose.Font for .NET | 23.12 或更高 |
| License | 有效的 .lic 文件(试用版可选 – 否则 30 天评估版可用)。 |
将 NuGet 包添加到项目中:
1dotnet add package Aspose.Font核心 API 概念
| 类 / 枚举 | 用途 |
|---|---|
FontDefinition | 描述您想打开的字体类型(FontType.Type1)的 what。 |
FontFileDefinition | 描述字体所在的位置 – 文件系统 (FileSystemStreamSource)、内存 (ByteContentStreamSource) 或任何自定义流来源的 where。 |
FontType | 枚举,告诉 Aspose 预期的格式(CFF、TTF、Type1,…)。 |
Aspose.Font.Font.Open(fd) | 静态方法,读取定义并返回 IFont 实现(Type1Font)。 |
Type1Font | 具体类,提供 Type‑1 字体的字形、度量、编码和渲染功能。 |
从磁盘加载 Type‑1 字体
1using System;
2using System.IO;
3using Aspose.Font;
4using Aspose.Font.Sources;
5
6// Path to the .pfb or .pfa file
7string fileName = @"C:\Fonts\courier.pfb"; // change to your file
8
9// Ensure the file exists
10if (!File.Exists(fileName))
11 throw new FileNotFoundException($"Font file not found: {fileName}");
12
13// Build a FontDefinition that tells Aspose we are dealing with a Type‑1 font.
14FontDefinition fd = new FontDefinition(
15 FontType.Type1, // format hint
16 new FontFileDefinition(
17 "pfb", // extension hint (pfb or pfa)
18 new FileSystemStreamSource(fileName))); // reads directly from the file system
19
20// Load it
21Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
22
23// Verify that the font really loaded
24if (font == null)
25 throw new InvalidOperationException("Unable to load the Type‑1 font.");从内存加载 Type‑1
1byte[] fontBytes = File.ReadAllBytes(fileName);
2FontDefinition fd = new FontDefinition(
3 FontType.Type1,
4 new FontFileDefinition("pfb", new ByteContentStreamSource(fontBytes)));
5Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;提示:
\"pfb\"字符串仅为提示。对于 ASCII 编码的 Type‑1 文件,请使用\"pfa\"。
结论
- Aspose.Font 使加载和使用传统 Type‑1 字体变得简洁:定义来源(
FileSystemStreamSource、ByteContentStreamSource等),通过Aspose.Font.Font.Open打开,即可立即访问度量、编码表和强大的渲染 API。 - 相同的模式同样适用于 CFF 和 TrueType/OpenType 字体——只需将
FontType.Type1更改为相应的枚举值。