Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save aspose-com-gists/acfce581e895d92531f47dd05053107e 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

Convert a TrueType font to WOFF with saveToFormat, and convert a CFF font to TrueType with convert to change its name before saving. The examples use Aspose.Font for Java.

Using the examples

The samples use montserrat-bold.ttf and 29645.cff, replacing the Montserrat Regular and CenturyGothic fonts in the documentation. Supply getDataDir() and getOutputDir() for your application. Input paths reference the sibling TTF and CFF directories; the output directory must exist and be writable.

The WOFF example closes its output stream with try-with-resources. The advanced example obtains a TtfFont, changes its name, and saves the resulting TTF. Outputs are TtfToWoff_out1.woff and CffToTtf_out.ttf. These examples demonstrate the two conversion paths shown here.

Related Documentation

Related Resources

Requirements

  • JDK 8 or later and Maven for this example project
  • Aspose.Font for Java 26.8
Aspose.Font for Java – Font Conversion Examples
// Learn more: https://docs.aspose.com/font/java/convert/advanced-method-for-font-conversion/
String fontPath = Paths.get(getDataDir(), "..", "CFF", "29645.cff").normalize().toString();
FontDefinition fontDefinition = new FontDefinition(FontType.CFF,
new FontFileDefinition("cff", new FileSystemStreamSource(fontPath)));
Font font = Font.open(fontDefinition);
TtfFont destFont = (TtfFont) font.convert(FontType.TTF);
destFont.setFontName(font.getFontName() + "_Converted");
String outPath = Paths.get(getOutputDir(), "CffToTtf_out.ttf").toString();
destFont.save(outPath);
// Learn more: https://docs.aspose.com/font/java/convert/
String fontPath = Paths.get(getDataDir(), "..", "TTF", "montserrat-bold.ttf").normalize().toString();
FontDefinition fontDefinition = new FontDefinition(FontType.TTF,
new FontFileDefinition("ttf", new FileSystemStreamSource(fontPath)));
Font font = Font.open(fontDefinition);
String outPath = Paths.get(getOutputDir(), "TtfToWoff_out1.woff").toString();
try (FileOutputStream outStream = new FileOutputStream(outPath)) {
font.saveToFormat(outStream, FontSavingFormats.WOFF);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment