Free Support Forum - aspose.com

Adding TTF Font bitstreams to Aspose.Word

Hi,
We are using Aspose.Word in our Web Service to convert Word documents to PDFs. A new client is using a font that is not installed on our server. We would like to load this font on demand and then unload it, but that does not seem possible. We have, however, discovered that we can use FontSettings.GetFontsSources() and FontSettings.SetFontsSources() to load the fonts. We have a question: Is FontSettings.SetFontsSources() thread safe? If not, what precautions should we take if we were to use this approach in a multi-threaded environment? Is there another approach besides embedding the fonts in the Word documents (which increases our web traffic) and installing the fonts on the servers (which has scalability issues)?

@stephenjenkins,

Thanks for your inquiry. Aspose.Words does support multi-threading. The only thing you need to make sure is that always use separate Document instance per each thread. One thread should use one Document object.

Yes, you can use FontSettings.SetFontsSources in your case. Following code example shows how to set Aspose.Words to look for TrueType fonts in system folders as well as a custom defined folder when scanning for fonts.

Document doc = new Document(MyDir + "Rendering.doc");

// Retrieve the array of environment-dependent font sources that are searched by default. For example this will contain a "Windows\Fonts\" source on a Windows machines.
// We add this array to a new ArrayList to make adding or removing font entries much easier.
FontSettings fontSettings = new FontSettings();
ArrayList fontSources = new ArrayList(fontSettings.GetFontsSources());

// Add a new folder source which will instruct Aspose.Words to search the following folder for fonts. 
FolderFontSource folderFontSource = new FolderFontSource("C:\\MyFonts\\", true);

// Add the custom folder which contains our fonts to the list of existing font sources.
fontSources.Add(folderFontSource);

// Convert the Arraylist of source back into a primitive array of FontSource objects.
FontSourceBase[] updatedFontSources = (FontSourceBase[])fontSources.ToArray(typeof(FontSourceBase));

// Apply the new set of font sources to use.
fontSettings.SetFontsSources(updatedFontSources);

doc.Save(MyDir + "Rendering.SetFontsFolders Out.pdf");

Please use FontSettings.ResetFontSources method to reset the fonts sources to the system default. Hope this helps you.

2 posts were split to a new topic: Performance issue with Aspose.PDF