Convert WOFF to WOFF2 | Java

[ ]

If you need to transform Web OpenFont Format to its version 2.0, this article provides you with the necessary information.

Use the following statements:

    package com.aspose.font;

    import java.nio.file.Files;
    import java.nio.file.Paths;

    import com.aspose.font.ByteContentStreamSource;
    import com.aspose.font.FileSystemStreamSource;
    import com.aspose.font.Font;
    import com.aspose.font.FontDefinition;
    import com.aspose.font.FontFileDefinition;
    import com.aspose.font.FontSavingFormats;
    import com.aspose.font.FontType;
    import com.aspose.font.TtfFont;

If you want to learn the Aspose conversion fundamentals go to How to convert the font into the desired format? chapter to find them out.

Taking the next three steps you will convert WOFF to WOFF2:

  1. Open Woff font file.

        // Open woff font
        String fontPath = Paths.get(getDataDir(), "Montserrat-Regular.woff").toString();
        FontDefinition fontDefinition = new FontDefinition(FontType.TTF, new FontFileDefinition("woff", new FileSystemStreamSource(fontPath)));
        Font font =;

  2. Notify the output setting.

        // Woff2 output settings
        String outPath = Paths.get(getOutputDir(), "WoffToWoff2_out1.woff2").toString();
        FileOutputStream outStream = new FileOutputStream(outPath);

  3. Transform the font into WOFF2 format and save the result.

        // Convert ttf to woff2
        font.saveToFormat(outStream, FontSavingFormats.WOFF2);