Load, Extract Data and Save from TTF | Java
The examples use montserrat-bold.ttf. Adapt getDataDir() to your input directory and getOutputDir() to your output directory, preserving the relative paths shown in the snippets.
Load TrueType Font from Disc
Aspose.Font for Java API lets you read TrueType Font types from files stored in your digital storage. TrueType font files stored on disc can be loaded by using the following steps.
- Define a new object of FontDefinition class
- Specify FontType as TTF and FontFileDefinition as ttf
- Create a TtfFont object and open the font file from FontDefinition object defined in the earlier step
1String fileName = Paths.get(getDataDir(), "..", "TTF", "montserrat-bold.ttf").normalize().toString();
2FontDefinition fd = new FontDefinition(FontType.TTF,
3 new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
4TtfFont font = (TtfFont) Font.open(fd);Save TTF Font
Once you load a TTF file, you may work with the font file, update some parameters and would like to save the file to disc. Aspose.Font for Java lets you save TTF file to disc using the following steps.
- Define a new object of FontDefinition class
- Specify FontType as TTF and FontFileDefinition as ttf
- Create a TtfFont object and open the font file from FontDefinition object defined in the earlier step
- Make the required updates and save the updated Font file to disc using the TtfFont’s
save()method
1byte[] fontMemoryData = Files.readAllBytes(
2 Paths.get(getDataDir(), "..", "TTF", "montserrat-bold.ttf").normalize());
3FontDefinition fd = new FontDefinition(FontType.TTF,
4 new FontFileDefinition("ttf", new ByteContentStreamSource(fontMemoryData)));
5TtfFont font = (TtfFont) Font.open(fd);
6
7// Work with the loaded font, then save it to disk.
8String outputFile = Paths.get(getOutputDir(), "montserrat-bold-saved.ttf").toString();
9font.save(outputFile);To learn more examples go to Aspose.Font.Examples.sln solution, in the net-examples folder of the Aspose.Font Documentation Github repository.
Also, check our free online Font Manipulation Applications to see how the functionality can be implemented in a web solution.