Loading Type‑1 (PostScript) fonts | .NET

Loading Type‑1 fonts with Aspose.Font for .NET

Why Type‑1 fonts?

What you need

ItemMinimum version
.NET Framework4.6 (or .NET Core 2.x / .NET 5+)
Aspose.Font for .NET23.12 or later
LicenseA valid .lic file (optional for trial – otherwise a 30‑day evaluation works).

Add the NuGet package to your project:

1dotnet add package Aspose.Font

Core API Concepts

Class / EnumPurpose
FontDefinitionDescribes what kind of font you want to open (FontType.Type1).
FontFileDefinitionDescribes where the font lives – file system (FileSystemStreamSource), memory (ByteContentStreamSource), or any custom stream source.
FontTypeEnum 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).
Type1FontConcrete 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

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.