Loading Type‑1 (PostScript) fonts | .NET
Contents
[
Hide
Show
]Loading Type‑1 fonts with Aspose.Font for .NET
Why Type‑1 fonts?
- Type‑1 (PostScript) fonts – .pfb (binary) and .pfa (ASCII) – are still used in many legacy publishing and CAD workflows.
- Aspose.Font can read, analyse, render and convert these fonts without any external dependencies, making it ideal for server‑side processing and document generation.
What you need
| Item | Minimum version |
|---|---|
| .NET Framework | 4.6 (or .NET Core 2.x / .NET 5+) |
| Aspose.Font for .NET | 23.12 or later |
| License | A valid .lic file (optional for trial – otherwise a 30‑day evaluation works). |
Add the NuGet package to your project:
1dotnet add package Aspose.FontCore API Concepts
| Class / Enum | Purpose |
|---|---|
FontDefinition | Describes what kind of font you want to open (FontType.Type1). |
FontFileDefinition | Describes where the font lives – file system (FileSystemStreamSource), memory (ByteContentStreamSource), or any custom stream source. |
FontType | Enum that tells Aspose what format to expect (CFF, TTF, Type1, …). |
Aspose.Font.Font.Open(fd) | Static method that reads the definition and returns an IFont implementation (Type1Font). |
Type1Font | Concrete class exposing glyphs, metrics, encoding, and rendering capabilities for Type‑1 fonts. |
Loading a Type‑1 font from disk
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.");Loading Type-1 from memory
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;Tip: The
"pfb"string is only a hint. Use"pfa"for ASCII‑encoded Type‑1 files.
Conclusion
- Aspose.Font makes loading and working with legacy Type‑1 fonts straightforward: define the source (
FileSystemStreamSource,ByteContentStreamSource, …), open viaAspose.Font.Font.Open, and you instantly gain access to metrics, encoding tables, and powerful rendering APIs. - The same pattern applies to CFF and TrueType/OpenType fonts—simply change
FontType.Type1to the appropriate enum value.