Working with custom fonts

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”).

  1. 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");

    }

@samuelmartinucci,

I will try to replicate everything could you please provide that custom font?

I’ve just downloaded Open Sans - Google Fonts for a trial.

Firstt try to load the regular font, secondly, try to name it custom-OpenSans

@samuelmartinucci,

Here is a code sample that can guide you:

private void Logic()
{
    var fontSource = new FolderFontSource($@"{prefixPathFonts}\Open_Sans\static\OpenSans");

    FontRepository.Sources.Add(fontSource);

    var doc = new Document();

    var page = doc.Pages.Add();

    var font = FontRepository.FindFont("OpenSans-Bold");

    var textFragment = new TextFragment("Test OpenSans-Bold loaded using file");

    textFragment.TextState.FontSize = 12;

    textFragment.TextState.Font = font;

    textFragment.TextState.Font.IsEmbedded = true;

    textFragment.TextState.Font.IsSubset = true;

    page.Paragraphs.Add(textFragment);


    font = FontRepository.FindFont("OpenSans-Italic");

    textFragment = new TextFragment("Test OpenSans-Italic.ttf loaded using file");

    textFragment.TextState.FontSize = 12;

    textFragment.TextState.Font = font;

    textFragment.TextState.Font.IsEmbedded = true;

    textFragment.TextState.Font.IsSubset = true;

    page.Paragraphs.Add(textFragment);            


    doc.Save($"{PartialPath}_output.pdf");
}

Here is the output file:
UsingCustomFonts_output.pdf (51.3 KB)

Thanks for your reply but I still have questions.

  1. Your code doesn’t work if you load the fonts using FileFontSource, why does it happen?

    final var pdfFileFontSource = new FileFontSource(“$@”{prefixPathFonts}\Open_Sans\static\OpenSans\OpenSans-Regular.ttf");
    FontRepository.getSources().add(pdfFileFontSource);

  2. I am trying to decorate a PDF that may have an embedded font named “OpenSans”, which is not effectively OpenSans (i.e.: You can produce it using Glyphr Studio). To prevent it, I want to give the font I am embedding an alias, something like Custom-OpenSans. i.e.:

    var font = FontRepository.FindFont(“Custom-OpenSans-Bold”);
    var textFragment = new TextFragment(“Test Custom-OpenSans-Bold loaded using file”);

I am dealing with a wide range of PDF and there are extreme corner cases as the one I described.

@samuelmartinucci,

FileFontSource Seems to be failing in that regard.

Sadly, I do not know how to give a font an alias.

If you want to load fonts individually instead of in a folder, you can use the following method:

private void Logic2()
{
    var doc = new Document();

    var page = doc.Pages.Add();            

    var textFragment = new TextFragment("Test OpenSans-Bold loaded using file");

    textFragment.TextState.FontSize = 12;

    var font = FontRepository.OpenFont($@"{prefixPathFonts}\Open_Sans\static\OpenSans\OpenSans-Bold.ttf");

    textFragment.TextState.Font = font;

    textFragment.TextState.Font.IsEmbedded = true;

    textFragment.TextState.Font.IsSubset = true;

    page.Paragraphs.Add(textFragment);

    textFragment = new TextFragment("Test OpenSans-Italic.ttf loaded using file");

    textFragment.TextState.FontSize = 12;

    font = FontRepository.OpenFont($@"{prefixPathFonts}\Open_Sans\static\OpenSans\OpenSans-Italic.ttf");

    textFragment.TextState.Font = font;

    textFragment.TextState.Font.IsEmbedded = true;

    textFragment.TextState.Font.IsSubset = true;

    page.Paragraphs.Add(textFragment);


    doc.Save($"{PartialPath}_IndividualFonts_output.pdf");
}