Skip to main content

DXFontRepository Class

Stores registered fonts.

Declaration

public class DXFontRepository extends JavaObject implements IDisposable

Remarks

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.

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);
            }
        }
    }
}

Refer to the following help topic for additional information: Load and Manage Custom Fonts in Java Document Processing Applications.

Implements

com.devexpress.system.IDisposable

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
com.devexpress.system.JavaObject
DXFontRepository

Methods

addFont(byte[] fontData) Method

Adds a font from the specified byte array.

Declaration

public void addFont(byte[] fontData)

Parameters

Name Type Description
fontData byte[]

The font data byte array.

addFont(InputStream fontStream) Method

Adds a font from the specified stream.

Declaration

public void addFont(InputStream fontStream)

Parameters

Name Type Description
fontStream InputStream

The font data stream.

addFont(ReadableByteChannel channel) Method

Adds a font from the specified channel.

Declaration

public void addFont(ReadableByteChannel channel)

Parameters

Name Type Description
channel ReadableByteChannel

The readable byte channel for the font data.

addFont(String fontFileName) Method

Adds a font from the specified file path.

Declaration

public void addFont(String fontFileName)

Parameters

Name Type Description
fontFileName String

The font file path.

addQueryNotFoundFontEventHandler(String key, GenericEventHandler<NotFoundFontEventArgs> value) Method

Adds the specified event handler.

Declaration

public static void addQueryNotFoundFontEventHandler(String key, GenericEventHandler<NotFoundFontEventArgs> value)

Parameters

Name Type Description
key String

The handler key.

value com.devexpress.system.GenericEventHandler<NotFoundFontEventArgs>

The event handler.

Remarks

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);
            }
        }
    }
}

Refer to the following help topic for additional information: Load and Manage Custom Fonts in Java Document Processing Applications.

clear() Method

Removes all fonts from the repository.

Declaration

public void clear()

Remarks

Call the DXFontRepository.clear() method to remove all fonts from the repository:

if (!DXFontRepository.getInstance().getIsEmpty()) {
    DXFontRepository.getInstance().clear();
}

close() Method

Releases resources used by this font repository.

Declaration

public void close()

getFonts() Method

Returns registered fonts.

Declaration

public List<DXFontData> getFonts()

Returns

Type Description
java.util.List<DXFontData>

Registered fonts.

Remarks

Use the DXFontRepository.getFonts() method to access information about fonts registered in the repository.

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());
    }
}

Refer to the following help topic for additional information: Load and Manage Custom Fonts in Java Document Processing Applications.

getInstance() Method

Returns the font repository instance.

Declaration

public static DXFontRepository getInstance()

Returns

Type Description
DXFontRepository

The font repository instance.

getIsEmpty() Method

Returns whether the repository contains fonts.

Declaration

public boolean getIsEmpty()

Returns

Type Description
boolean

true if the repository contains no fonts; otherwise, false.

removeQueryNotFoundFontEventHandler(String key) Method

Removes the specified event handler.

Declaration

public static void removeQueryNotFoundFontEventHandler(String key)

Parameters

Name Type Description
key String

The handler key.