I am trying to work on a solution where there are 2 requirements:
1. Load a custom font from a folder containing hundreds of fonts.
I’ve tried the following code (and the font is not loaded, I get class com.aspose.pdf.exceptions.PdfException: Font OpenSans was not found
):
final var pdfFileFontSource = new FileFontSource("/Users/martinucci/Downloads/Open_Sans/static/OpenSans/OpenSans-Regular.ttf");
FontRepository.getSources().add(pdfFileFontSource);
The following code runs much faster and the font is properly loaded:
final var pdfFileFontSource = new FolderFontSource("/Users/martinucci/Downloads/Open_Sans/static/OpenSans");
FontRepository.getSources().add(pdfFileFontSource);
IMPORTANT: The only thing in the directory is the file I loaded formerly and the error arises from a call to FontRepositor.findFont(“OpenSans”).
- I need to be able to give an alias to a font and embed it. Let’s say I am modifying a document that has text using OpenSans font, I want that text to remain as-is, but newly added text that is OpenSans I want it to have a font called Custom-OpenSans. Is it possible?
I am running Aspose.PDF 23.2 and the complete code can be found below:
public static void main(String[] args) {
// final var pdfFileFontSource = new FileFontSource("/Users/martinucci/Downloads/Open_Sans/static/OpenSans/OpenSans-Regular.ttf");
final var pdfFileFontSource = new FolderFontSource("/Users/martinucci/Downloads/Open_Sans/static/OpenSans");
FontRepository.getSources().add(pdfFileFontSource);
var doc = new Document();
var page = doc.getPages().add();
var textFragment = new TextFragment("Test OpenSans loaded using file");
textFragment.getTextState().setFont(FontRepository.findFont("OpenSans", FontStyles.Regular));
page.getParagraphs().add(textFragment);
doc.save("test.pdf");
}