Load and Manage Custom Fonts in Java Document Processing Applications
- 4 minutes to read
A document can use fonts that are unavailable in the environment where your application runs. For example, a custom font may be installed on a development machine but unavailable on a server, in a Docker container, or on a client machine.
The Office & PDF File API can load custom fonts at runtime. Add the required font files to the DXFontRepository before you load or create a document. The API uses these fonts when it processes the document.
Note
Fonts that you add to the repository are not stored in the document. The application supplies the font data at runtime. If a required font is unavailable in the target environment, the API uses the repository font instead of a substitute font.
Supported Font Formats
The following font formats are supported:
- TrueType fonts (
.TTF) - OpenType fonts that use CFF (Compact Font Format) glyph outlines (
.OTF) - OpenType Font Collections (
.TTC,.OTC) that contain multiple fonts in a single file
The following font formats and types are not supported:
- Variable fonts (VF)
- TTE
- EUDC
Load Fonts into the Font Repository
Use the DXFontRepository.addFont() method to load a font from a file, stream, or byte array.
The following code snippet loads a custom font from an application file and registers the font in DXFontRepository before the application loads the presentation. The API uses the font when it processes and exports the presentation.
import com.devexpress.docs.presentation.Presentation;
import com.devexpress.drawing.DXFontRepository;
import java.io.*;
import java.nio.file.*;
public class Application {
public static void main(String[] args) throws IOException {
// Register the font used in the presentation.
DXFontRepository.getInstance().addFont("fonts/CUSTOM_FONT_NAME.ttf");
try (FileInputStream inputStream = new FileInputStream("Presentation.pptx");
Presentation presentation = new Presentation(inputStream)) {
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
Path pdfPath = outputDir.resolve("Presentation.pdf");
// Export the presentation to PDF.
try (FileOutputStream stream = new FileOutputStream(pdfPath.toFile())) {
presentation.exportToPdf(stream);
}
}
}
}
Note
Loaded fonts are not saved to the document. The application uses these fonts at runtime.
Handle Missing Fonts
A document can reference a font that is neither installed on the operating system nor registered in DXFontRepository. The Office & PDF File API then substitutes an available font for the missing font.
Important
Font substitution can change the document layout and appearance.
Add the DXFontRepository.addQueryNotFoundFontEventHandler to detect missing fonts and supply the required font data at runtime.
The following code snippet registers the custom font when the application cannot find it:
import com.devexpress.docs.presentation.Presentation;
import com.devexpress.drawing.DXFontRepository;
import java.io.*;
import java.nio.file.*;
public class Application {
public static void main(String[] args) throws IOException {
DXFontRepository.addQueryNotFoundFontEventHandler(
"CustomFontHandler",
(sender, event) -> {
if ("CUSTOM_FONT_NAME".equals(event.getRequestedFont())) {
try {
byte[] fontData = Files.readAllBytes(Path.of("fonts/CUSTOM_FONT_NAME.ttf"));
event.setFontFileData(fontData);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
);
try (FileInputStream inputStream = new FileInputStream("Presentation.pptx");
Presentation presentation = new Presentation(inputStream)) {
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
Path pdfPath = outputDir.resolve("Presentation.pdf");
// Export the presentation to PDF.
try (FileOutputStream stream = new FileOutputStream(pdfPath.toFile())) {
presentation.exportToPdf(stream);
}
}
}
}
Load Missing Fonts from a Font Hosting Service
Use the missing-font notification mechanism to load required fonts from a font hosting service (for example, Google Fonts).
When the application detects a missing font:
- Identify the requested font.
- Download the corresponding font file.
- Pass the font data to event arguments.
- Continue document processing.
Warning
Review the license terms for each font that you use. Font licenses can restrict redistribution and commercial use.
Access Information About Repository Fonts
Use the DXFontRepository.getFonts() method to access information about fonts registered in the repository.
Call the DXFontRepository.getIsEmpty() method to check whether the repository contains fonts.
if (DXFontRepository.getInstance().getIsEmpty()) {
System.out.println("Font repository is empty.");
} else {
System.out.println("Font repository contains the following fonts:");
var fonts = DXFontRepository.getInstance().getFonts();
for (var font : fonts) {
System.out.println(" * " + font.getName());
}
}
Clear the Font Repository
Call the DXFontRepository.clear() method to remove all fonts from the repository:
if (!DXFontRepository.getInstance().getIsEmpty()) {
DXFontRepository.getInstance().clear();
}