Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/e040fce8f0be259e85593b1866e9c4a6 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/e040fce8f0be259e85593b1866e9c4a6 to your computer and use it in GitHub Desktop.
Conversion overview and advanced conversion using Convert and SaveToFormat; supported formats require validation during migration.

Font Conversion Examples

Conversion overview and advanced conversion using Convert and SaveToFormat; supported formats require validation during migration.

Status

Two documentation examples are implemented: TTF to WOFF with SaveToFormat, and CFF to TTF with Convert followed by renaming and saving. Runtime verification is pending the user-run tests.

Running the examples

From the repository root:

dotnet test src/Aspose.Font.Net/Aspose.Font.Tests.csproj --filter "TestCategory=ConvertFontExamples"

Inputs under tests-data/Aspose.Font: TTF/montserrat-bold.ttf and CFF/29645.cff. BaseTests supplies the license setup and resolves DataDir and OutputDir. Outputs under tests-out/Aspose.Font.Net/ConvertFonts: TtfToWoff_out1.woff and CffToTtf_out.ttf. Assertions check saved files, reopen both outputs, and verify the converted font name. Convert targets TrueType here; these examples do not validate every format pair listed in the documentation.

Related Documentation

Related Resources

Requirements

  • .NET 5.0+, .NET Core, or .NET Framework
  • Aspose.Font for .NET library
Aspose.Font for .NET – Font Conversion Examples
// Convert a CFF font to TTF and change its name before saving
// Learn more: https://docs.aspose.com/font/net/convert/advanced-method-for-font-conversion/
// Open the CFF font
string fontPath = Path.Combine(DataDir, "CFF", "29645.cff");
FontDefinition fontDefinition = new FontDefinition(
FontType.CFF, new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)));
Font font = Font.Open(fontDefinition);
// Convert to TrueType and obtain the format-specific font object
TtfFont destFont = font.Convert(FontType.TTF) as TtfFont
?? throw new InvalidOperationException("Conversion did not return a TrueType font.");
// Change the converted font name before saving
destFont.FontName = font.FontName + "_Converted";
// Save the renamed TrueType font
string outPath = Path.Combine(OutputDir, "CffToTtf_out.ttf");
destFont.Save(outPath);
// Convert a TTF font to WOFF
// Learn more: https://docs.aspose.com/font/net/convert/
// Open the TTF font
string fontPath = Path.Combine(DataDir, "TTF", "montserrat-bold.ttf");
FontDefinition fontDefinition = new FontDefinition(
FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fontPath)));
Font font = Font.Open(fontDefinition);
// Convert to WOFF and close the output stream after saving
string outPath = Path.Combine(OutputDir, "TtfToWoff_out1.woff");
using (FileStream outStream = File.Create(outPath))
{
font.SaveToFormat(outStream, FontSavingFormats.WOFF);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment