Custom PowerPoint Font

Load Custom Fonts

Aspose.Slides allows you to load fonts that are rendered in presentations without having to install those fonts. The fonts are loaded from a custom directory.

  1. Create an instance of the FontsLoader class and call the loadExternalFonts method.
  2. Load the presentation that will be rendered.
  3. Clear the cache in the FontsLoader class.

This PHP code demonstrates the font loading process:

  # Folders to seek fonts
  $folders = array($externalFontsDir );
  # Loads the custom font directory fonts
  FontsLoader->loadExternalFonts($folders);
  # Do Some work and perform presentation/slide rendering
  $pres = new Presentation("DefaultFonts.pptx");
  try {
    $pres->save("NewFonts_out.pptx", SaveFormat::Pptx);
  } finally {
    if (!java_is_null($pres)) {
      $pres->dispose();
    }
    # Clears Font Cachce
    FontsLoader->clearCache();
  }

Get Custom Fonts Folder

Aspose.Slides provides the getFontFolders method to allow you to find font folders. This method returns folders added through the LoadExternalFonts method and system font folders.

This PHP code shows you how to use getFontFolders:

  # This line outputs folders where font files are searched.
  # Those are folders added through the LoadExternalFonts method and system font folders.
  $fontFolders = FontsLoader->getFontFolders();

Specify Custom Fonts Used With Presentation

Aspose.Slides provides the setDocumentLevelFontSources property to allow you to specify external fonts that will be used with the presentation.

This PHP code shows you how to use the setDocumentLevelFontSources property:

  $Array = new JavaClass("java.lang.reflect.Array");
  $Byte = new JavaClass("java.lang.Byte");
  $file1 = new Java("java.io.File", "customfonts/CustomFont1.ttf");
  $memoryFont1 = $Array->newInstance($Byte, $Array->getLength($file1));
  try {
      $dis1 = new Java("java.io.DataInputStream", new Java("java.io.FileInputStream", $file1));
      $dis1->readFully($memoryFont1);
  } finally {
      if (!java_is_null($dis1)) $dis1->close();
  }
  $file2 = new Java("java.io.File", "customfonts/CustomFont2.ttf");
  $memoryFont2 = $Array->newInstance($Byte, $Array->getLength($file2));
  try {
        $dis2 = new Java("java.io.DataInputStream", new Java("java.io.FileInputStream", $file2));
        $dis2->readFully($memoryFont2);
  } finally {
        if (!java_is_null($dis2)) $dis2->close();
  }
  $loadOptions = new LoadOptions();
  $loadOptions->getDocumentLevelFontSources()->setFontFolders(array("assets/fonts", "global/fonts" ));
  $loadOptions->getDocumentLevelFontSources()->setMemoryFonts(array($memoryFont1, $memoryFont2 ));
  $pres = new Presentation("MyPresentation.pptx", $loadOptions);
  try {
    # Work with the presentation
    # CustomFont1, CustomFont2, and fonts from assets\fonts & global\fonts folders and their subfolders are available to the presentation
  } finally {
    if (!java_is_null($pres)) {
      $pres->dispose();
    }
  }

Manage Fonts Externally

Aspose.Slides provides the loadExternalFont(byte[] data) method to allow you to load external fonts from binary data.

This PHP code demonstrates the byte array font loading process:

$Array = new JavaClass("java.lang.reflect.Array");
$Byte = (new JavaClass("java.lang.Byte"))->TYPE;
try {
    $dis = new Java("java.io.DataInputStream", new Java("java.io.FileInputStream", "ARIALN.TTF"));
    $bytes = $Array->newInstance($Byte, $dis->available());
    $dis->readFully($bytes);
} finally {
    if (!java_is_null($dis)) $dis->close();
}
  FontsLoader->loadExternalFont($bytes);

try {
    $dis = new Java("java.io.DataInputStream", new Java("java.io.FileInputStream", "ARIALNBI.TTF"));
    $bytes = $Array->newInstance($Byte, $dis->available());
    $dis->readFully($bytes);
} finally {
    if (!java_is_null($dis)) $dis->close();
}
  FontsLoader->loadExternalFont($bytes);

try {
    $dis = new Java("java.io.DataInputStream", new Java("java.io.FileInputStream", "ARIALNI.TTF"));
    $bytes = $Array->newInstance($Byte, $dis->available());
    $dis->readFully($bytes);
} finally {
    if (!java_is_null($dis)) $dis->close();
}
  FontsLoader->loadExternalFont($bytes);

  try {
    $pres = new Presentation("");
    try {
      # external font loaded during the presentation lifetime
    } finally {
    }
  } finally {
    FontsLoader->clearCache();
  }