Fonts sources search caching in aspose words for java

Hi,

In my search to limit file access when rebuilding caches on FontSettings instances, my eye fell on the Aspose.Words for .net method FontSettings.SetFontsSources(FontSourceBase[], Stream), which look like to be exactly what I need.

However, I could not find a counterpart in the java libraries. Is there something available in java or another way to achieve the same?

Thanks,
Steven.

@svanloon The same API is available in Aspose.Words for Java starting from 21.12 version. We will fix the corresponding section in Java version API references. You can use this feature using the code like the following:

// Prepare font sources and generate font search cache beforehand.
FileFontSource fileSource = new FileFontSource(filePath, fileSourcePriority, fileSourceKey);
MemoryFontSource memorySource = new MemoryFontSource(fontData, memorySourcePriority, memorySourceKey);
StreamFontSource streamSource = new SteamFontSourceMemoryImpl(streamSourcePriority, streamSourceKey);
FontSettings settings = new FontSettings();
settings.setFontsSources(new FontSourceBase[] { fileSource, memorySource, streamSource });
settings.saveSearchCache(cacheOutputStream);

// Set font sources and load search cache before processing documents. Note that sources should be the same as when saving font search cache.
FileFontSource fileSource = new FileFontSource(filePath, fileSourcePriority, fileSourceKey);
MemoryFontSource memorySource = new MemoryFontSource(fontData, memorySourcePriority, memorySourceKey);
StreamFontSource streamSource = new SteamFontSourceMemoryImpl(streamSourcePriority, streamSourceKey);
FontSettings settings = new FontSettings();
settings.setFontsSources(new FontSourceBase[] { fileSource, memorySource, streamSource }, cacheInputStream);

Ok, thanks a lot, I will try this out!

Hi Alexey,

The saveSearchCache methods takes an InputStream as input, which is a bit weird because I would expect an OutputStream. Do you have an example on how to actually use this? I would like to write that cache to memory, something like this:

		byte[] searchCache;
		try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
			settings.saveSearchCache(os);
			os.flush();
			searchCache = os.toByteArray();
		}

		try (ByteArrayInputStream is = new ByteArrayInputStream(searchCache)) {
			settings.setFontsSources(new FontSourceBase[]{new SystemFontSource()}, is);
		}

but that seems to be not possible. Do you have any suggestion how to use?

Thanks,
Steven.

@svanloon It seems there was a mistake upon porting this feature from .NET to Java version of Aspose.Words. I have logged this problem as WORDSJAVA-2680. We will keep you informed and let you know once it is resolved. Please accept our apologizes for inconvenience and thank you for pointing us to the problem.

Ok, thanks for the feedback.

The issues you have found earlier (filed as WORDSJAVA-2680) have been fixed in this Aspose.Words for Java 22.9 update also available on Maven.