Font Conversion. Advanced details | .NET
In some cases, there is a sense to read/modify converted font data before saving the font.
Method Aspose.Font.Font.Convert(FontType fontType) was designed for such cases. This method converts a font into the type specified and returns the object inherited from Aspose.Font.Font class which corresponds to FontType value, passed into Convert() method.
The next table shows a map of coherence between FontType values and objects, inherited from the base Aspose.Font.Font class.
Font type | Font object |
---|---|
TTF | Aspose.Font.Ttf.TtfFont |
Type1 | Aspose.Font.Type1.Type1Font |
CFF | Aspose.Font.Cff.CffFont |
OTF | Aspose.Font.Ttf.TtfFont |
Use resultant font object to access/change font properties before saving resultant font or instead of saving resultant font.
At the current moment method
Convert() supports conversion only into TrueType
font format (FontType.TTF), so it always returns
the object of type
TtfFont as result of conversion independently of source font used.
Next code snippet loads CFF
font CenturyGothic
from disk, converts it into TrueType
format, and changes the name of the converted font to “CenturyGothic_Converted”.
Fulfill the next actions:
- Open the font.
- Convert the font into
TrueType
format. - Change the name of the converted font.
- Notify the output settings.
- Save the resultant with the just changed name.
1
2 // Open cff font
3 string fontPath = Path.Combine(DataDir, "CenturyGothic.cff");
4 FontDefinition fontDefinition = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)));
5 Font font = Font.Open(fontDefinition);
6
7 // Convert font into TrueType format and cast font returned to Aspose.Font.Ttf.TtfFont
8 Aspose.Font.Ttf.TtfFont destFont = font.Convert(FontType.TTF) as Aspose.Font.Ttf.TtfFont;
9
10 // Change name of converted font
11 destFont.FontName = "CenturyGothic_Converted";
12
13 // Ttf output settings
14 string outPath = Path.Combine(OutputDir, "CffToTtf_out.ttf");
15
16 // Save resultant font with font name changed
17 destFont.Save(outPath);
The full range of examples for using Aspose.Font for.NET is placed in Aspose.Font.Examples.sln solution, in the net-examples folder of the Aspose.Font Documentation github repository.