正在加载 Type-1 (PostScript) 字体 | .NET

使用 Aspose.Font for .NET 加载 Type‑1 字体

为什么使用 Type‑1 字体?

所需条件

项目最低版本
.NET Framework4.6(或 .NET Core 2.x / .NET 5+)
Aspose.Font for .NET23.12 或更高
License有效的 .lic 文件(试用版可选 – 否则 30 天评估版可用)。

将 NuGet 包添加到项目中:

1dotnet add package Aspose.Font

核心 API 概念

类 / 枚举用途
FontDefinition描述您想打开的字体类型(FontType.Type1)的 what
FontFileDefinition描述字体所在的位置 – 文件系统 (FileSystemStreamSource)、内存 (ByteContentStreamSource) 或任何自定义流来源的 where
FontType枚举,告诉 Aspose 预期的格式(CFFTTFType1,…)。
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\"

结论

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.