Dynamically Adding Fonts Problem

I’m having issues making Cells find new fonts that are added at runtime in rendering a PDF. I’m needing to add them at runtime because we’re using a fonts repository that returns TTF files on-demand, as they’re found in documents, instead of all up-front.


I’ve tried many different approaches:
1. Adding the fonts to the directory specified in the “Aspose.Cells.FontDirExc” location
2. Adding the new fonts as MemoryFontSources to the FontSourceBase[] passed to FontConfigs.setFontSources(…).

I’ve noticed that as long as I haven’t performed ANY Cells calls yet, #1 will work correctly.
It seems as though FontConfigs.setFontSources will only accept ONE item of “MemoryFontSource” as a FontSourceBase. If I add multiple, only the first seems to be found. Should I be able to use more than one MemoryFontSource, or is that approach limited to just one font?

Is there a proper way to dynamically load fonts at runtime, after Cells has had calls in its stack, either from disk or from byte array?

Examples:
#1: Works. Assume document has: Arial, Calibri in it only (pseudo-code):
System.setProperty(“Aspose.Cells.FontDirExc”, tempFontsDir.toAbsolutePath().toString());

addFont(“Arial”); // addFont is custom code that downloads the font and stores it in the directory specified by the FontDirExec JVM arg

addFont(“Calibri”);
Workbook workbook = new Workbook(new ByteArrayInputStream(data));
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.save(os, new PdfSaveOptions());

#2: Does NOT work. Same document: Arial, Calibri:
System.setProperty(“Aspose.Cells.FontDirExc”, tempFontsDir.toAbsolutePath().toString());

addFont(“Arial”); // addFont is custom code that downloads the font and stores it in the directory specified by the FontDirExec JVM arg

<span style=“font-family: “Courier New”; font-size: small;”>Workbook workbook = new Workbook(new ByteArrayInputStream(data));
<span style=“font-family: “Courier New”; font-size: small;”>
addFont(“Calibri”);

ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.save(os, new PdfSaveOptions());
Hi,

Thanks for your posting and using Aspose.Cells.

You can try the following code:

Java
String fontDir =.;//your font dır;
addFont("Arial");
FontConfigs.setFontExclusiveSources(new FontSourceBase[] {new FolderFontSource(fontDir, false)});
Workbook wb = new Workbook("d:/temp/test.xlsx");
addFont("Calibri");
FontConfigs.setFontExclusiveSources(new FontSourceBase[] {new FolderFontSource(fontDir, false)});
wb.save(TMP_DIR + "test_Java.pdf");

But we suggest do

"FontConfigs.setFontExclusiveSources(new FontSourceBase[] {new FolderFontSource(fontDir, false)});

at the start or at least before a workbook init.

Like:
1 - Add some fonts
2 - Call the following
FontConfigs.setFontExclusiveSources(new FontSourceBase[] {new FolderFontSource(fontDir, false)});
3 - Init a workbook
4 - Save the workbook to pdf

Or you may do or repeat like
1 - Add some fonts
2 - Call the following
FontConfigs.setFontExclusiveSources(new FontSourceBase[] {new FolderFontSource(fontDir, false)});
3 - Init another workbook
4 - Save the workbook to pdf

FontConfigs.setFontExclusiveSources(new FontSourceBase[] {new FolderFontSource(fontDir, false)});
used after a workbook init or before save to pdf is not suggested.

For your issue

"It seems as though FontConfigs.setFontSources will only accept ONE item of "MemoryFontSource" as a FontSourceBase. If I add multiple, only the first seems to be found. Should I be able to use more than one MemoryFontSource, or is that approach limited to just one font?"

We tested and it works OK using
FontConfigs.setFontSources(new FontSourceBase[]{new MemoryFontSource(font data)), new MemoryFontSource(another font data)});

This seems to be working if I use the “setFontExclusiveSources” instead of the “setFontSources”. Thank you!


Can you explain the difference between those two functions?
Hi,

Thanks for using Aspose.Cells.

It means, that you want to restrict Aspose.Cells to search for fonts from your specific location. It is same as

Aspose.Cells.FontDirExc

in your first post. Let us know if you face any issue. We will look into it and help you out.