Embedded Font - PowerPoint Java API

Embedded fonts in PowerPoint are useful when you want your presentation to appear correctly when opened on any system or device. If you used a third-party or non-standard font because you got creative with your work, then you have even more reasons to embed your font. Otherwise (without embedded fonts), the texts or numbers on your slides, the layout, styling, etc. may change or turn into confusing rectangles.

The FontsManager class, FontData class, Compress class, and their interfaces contain most of the properties and methods you need to work with embedded fonts in PowerPoint presentations.

Get or Remove Embedded Fonts from Presentation

Aspose.Slides provides the getEmbeddedFonts method (exposed by the FontsManager class) to allow you to get (or find out) the fonts embedded in a presentation. To remove fonts, the removeEmbeddedFont method (exposed by the same class) is used.

This Java code shows you how to get and remove embedded fonts from a presentation:

// Instantiates a Presentation object that represents a presentation file
Presentation pres = new Presentation("EmbeddedFonts.pptx");
try {
    // Renders a slide containing a text frame that uses embedded "FunSized"
    ImageIO.write(pres.getSlides().get_Item(0).getThumbnail(new Dimension(960, 720)),
            "PNG", new File("picture1_out.png"));

    IFontsManager fontsManager = pres.getFontsManager();

    // Gets all embedded fonts
    IFontData[] embeddedFonts = fontsManager.getEmbeddedFonts();

    // Finds the "Calibri" font
    IFontData calibriEmbeddedFont = null;
    for (int i = 0; i < embeddedFonts.length; i++) {
        System.out.println(""+ embeddedFonts[i].getFontName());
        if ("Calibri".equals(embeddedFonts[i].getFontName())) {
            calibriEmbeddedFont = embeddedFonts[i];
            break;
        }
    }

    // Removes "Calibri" font
    fontsManager.removeEmbeddedFont(calibriEmbeddedFont);

    // Renders the presentation; "Calibri" font is replaced with an existing one
    ImageIO.write(pres.getSlides().get_Item(0).getThumbnail(new Dimension(960, 720)),
            "PNG", new File("picture2_out.png"));

    // Saves the presentation without embedded "Calibri" font to disk
    pres.save("WithoutManageEmbeddedFonts_out.ppt", SaveFormat.Ppt);
} catch(IOException e) {
} finally {
    if (pres != null) pres.dispose();
}

Add Embedded Fonts to Presentation

Using the EmbedFontCharacters enum and two overloads of the addEmbeddedFont method, you can select your preferred (embedding) rule to embed the fonts in a presentation. This Java code shows you how to embed and add fonts to a presentation:

// Loads the presentation
Presentation pres = new Presentation("Fonts.pptx");
try {
    IFontData[] allFonts = pres.getFontsManager().getFonts();
    IFontData[] embeddedFonts = pres.getFontsManager().getEmbeddedFonts();

    for (IFontData font : allFonts)
    {
        boolean embeddedFontsContainsFont = false;
        for (int i = 0; i < embeddedFonts.length; i++)
        {
            if (embeddedFonts[i].equals(font))
            {
                embeddedFontsContainsFont = true;
                break;
            }
        }
        if (!embeddedFontsContainsFont)
        {
            pres.getFontsManager().addEmbeddedFont(font, EmbedFontCharacters.All);

            embeddedFonts = pres.getFontsManager().getEmbeddedFonts();
        }
    }

    // Saves the presentation to disk
    pres.save("AddEmbeddedFont_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

Compress Embedded Fonts

To allow you to compress the fonts embedded in a presentation and reduce its file size, Aspose.Slides provides the compressEmbeddedFonts method (exposed by the Compress class).

This Java code shows you how to compress embedded PowerPoint fonts:

Presentation pres = new Presentation("pres.pptx");
try {
    Compress.compressEmbeddedFonts(pres);
    pres.save("pres-out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}