Overview
Many document formats are tightly bound with fonts. For example, when a user adds some text to the DOCX document, this text always has some defined font. Some document formats usually store the font data embedded in the document file itself, like PDF. But others, like Office family formats, usually rely on the fonts installed in the operating system.
There are also plenty of font formats. For example, most installed fonts are presented in TTF (TrueType) and OTF (OpenType) formats. Office Open XML documents also work with EOT (Embedded OpenType) format. On the web, however, WOFF and WOFF2 are the most used formats.
Table of contents
- Overview
- Getting all used fonts
- Processing the missing fonts
- Specifying missing font
- Replacing the missing font
- Excluding the fonts for HTML
- Conclusion
- See also
- Get a free trial
Getting all used fonts
The most useful feature in the context of fonts is to list and return them. GroupDocs.Viewer provides a special method GetAllFonts()
— when the document is loaded to the instance of the Viewer
class, call this method to obtain a list of all fonts, used in this document. In the returned list every font will be presented as in instance of the UsedFontInfo
struct (value type), which stores the font family name, style, format, its binary content and also a flag that indicates where this font is stored (is it embedded in the loaded document body or installed in the OS).
Short example is below:
using GroupDocs.Viewer;
// ...
const string filename = "sample.docx";
string inputPath = "\full\path\" + filename;
using (Viewer viewer = new Viewer(inputPath))
{
IReadOnlyList<Fonts.UsedFontInfo> allFonts = viewer.GetAllFonts();
Console.WriteLine("{0} fonts found in the '{1}' document", allFonts.Count, filename);
foreach (Fonts.UsedFontInfo font in allFonts)
{
Console.WriteLine("{0} font '{1}' of '{2}' style has {3} bytes and is of '{4}' format",
font.IsEmbedded ? "Embedded" : "System",
font.Name,
font.Style,
font.Content.Length,
font.Format);
}
}
Processing the missing fonts
The situation, when some document was created on the author’s machine and uses some font “AAA”, and then is processed using the GroupDocs.Viewer on some client’s machine, where font “AAA” is missing, is typical. In such case GroupDocs.Viewer tries to substitute this font using some complex font substitution rules: it analyzes the metadata of the document, alternative names of the missing font, settings of the document, settings of the OS, list of all available fonts, installed in the OS and so on. Finally, when by some reason the OS is “clean”, when it has no installed fonts at all, then GroupDocs.Viewer will substitute the missing font with the one, that is embedded inside the GroupDocs.Viewer assembly (DLL); this font will serve as a “last resort” in case when no other fonts are available. For example, for the WordProcessing family formats, the GroupDocs.Viewer internally stores the “Fanwood” free font.
At this moment the GroupDocs.Viewer does the font substitution operation “silently” during rendering of the document (when the Viewer.View()
method is invoked) — there are no any messages or events when the font substitution mechanism is activated and which font is missing and onto which is replaced/substituted, and for which exact document. Such feature is hard to implement because it is too unclear, vague, and ambiguous what kind of notification should be emitted, in which form and how they can be utilizing on the client’s side.
However, the GroupDocs.Viewer provides the users two possible ways to solve such a situation: manually specify the missing font and manually substitute the missing font.
Specifying missing font
When the user initially knows that some document uses font “AAA”, and also knows that this font “AAA” is not installed on the target machine, where document should be rendered, and user has the binary content of this font “AAA”, he can specify it before calling the Viewer.View()
method.
In order to specify one or many custom fonts, which are not installed in the target operating system, user should add custom font source(s). Font source is actually a folder, where one or many fonts are located. It is represented by a FolderFontSource
class. And the FontSettings
class has a method SetFontSources()
for adding the font sources to the GroupDocs.Viewer.
The following code snippet demonstrates this. Let’s assume that the “sample.docx” document uses some rare font “AAA.ttf”, that is not installed on the target operating system. Then the user puts the file “AAA.ttf” to the "C:\custom_fonts"
folder and then creates an instance of the FolderFontSource
class that points to this folder. Then this instance is passed to the FontSettings
static class, and then the “sample.docx” file is rendered to HTML format. As a result, the “AAA” font will be used in the output HTML document.
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
using GroupDocs.Viewer.Fonts;
// ...
// Specify the font source.
FolderFontSource fontSource =
new FolderFontSource(@"C:\custom_fonts", SearchOption.TopFolderOnly);
FontSettings.SetFontSources(fontSource);
using (Viewer viewer = new Viewer("sample.docx"))
{
// Create an HTML file.
HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources();
viewer.View(viewOptions);
}
Replacing the missing font
When the user initially knows that some document uses font “AAA”, and also knows that this font “AAA” is not installed on the target machine, where document should be rendered, but user has no access to the binary content of this font “AAA”, he can specify another font “BBB”, that will be used instead of the missing “AAA”. Unlike the previous case, this should be done using the view options, which then are passed to the Viewer.View()
method.
This option is called a “DefaultFontName
”, it is a property of a string type, declared in the abstract class BaseViewOptions
, so it is common for all rendering options: HtmlViewerOptions
, PdfViewOptions
, PngViewOptions
, and JpgViewOptions
.
When this option is specified, the GroupDocs.Viewer uses this font during rendering instead of any not available fonts. For example, if the document includes a font containing non-English characters, specify the default font name to ensure that GroupDocs.Viewer replaces any missing font with one which has the same character set. Of course, the font, which name is specified in the DefaultFontName
property, should be installed on the operating system, where the GroupDocs.Viewer renders the document.
The following code snippet shows how to set the default font name:
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...
using (Viewer viewer = new Viewer("sample.pptx"))
{
// Create options for rendering HTML document with embedded resources
HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources();
// Specify a default font name is the options
viewOptions.DefaultFontName = "Courier New";
// Render input PPTX to output HTML
viewer.View(viewOptions);
}
Excluding the fonts for HTML
When rendering the documents to the HTML format, GroupDocs.Viewer by default exports all used fonts to the HTML document. This ensures correct display regardless of the required fonts existing on the viewer device. Fonts can be stored in the resultant HTML document as external resources (HtmlViewOptions.ForExternalResources
) or be embedded directly into HTML-markup using the data URI scheme through base64 encoding (HtmlViewOptions.ForEmbeddedResources
). Font exporting to HTML format is supported by almost all document formats that support fonts itself: Microsoft Office (except Excel), OpenDocument formats, emails, PDF, eBooks and so on.
However sometimes this feature is unwanted. Actually, when fonts are included, they significantly increase the size of the produced HTML document. So when the target machine or device, where the HTML document should be displayed, has all the used fonts to be installed, the browser can use these system-installed fonts.
In order to solve such demands the GroupDocs.Viewer has an ability to exclude fonts from the produced HTML document, and this can be done in two ways: exclude all fonts and exclude specific fonts only.
Both these options are represented in the HtmlViewOptions
class as different properties:
ExcludeFonts
is a flag that disables export of all used fonts to the produced HTML document, when enabled (true
). By default this option is disabled (false
).FontsToExclude
is a list of string, each one represents one font name, that should be excluded from the produced HTML document. By default this list is empty — no fonts are excluded. Please note that this option works only if theExcludeFonts
property is disabled (false
).
Two code snippets below demonstrate both these possibilities: first one shows using the ExcludeFonts
, while second — FontsToExclude
.
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...
using (Viewer viewer = new Viewer("sample.docx"))
{
// Create an HTML file.
var viewOptions = HtmlViewOptions.ForEmbeddedResources();
viewOptions.ExcludeFonts = true;
viewer.View(viewOptions);
}
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...
using (Viewer viewer = new Viewer("presentation.pptx"))
{
// Create an HTML file.
var viewOptions = HtmlViewOptions.ForEmbeddedResources();
viewOptions.FontsToExclude.Add("Times New Roman");// Exclude the Times New Roman font.
viewOptions.FontsToExclude.Add("Arial");// Exclude the Arial font.
viewer.View(viewOptions);
}
Conclusion
The GroupDocs.Viewer is primarily focused on rendering documents of different formats into the commonly widespread “viewable” formats — HTML, PDF, JPEG, and PNG. But during the last period of time we added several useful features regarding font processing that allow to review and examine used fonts, tune-up their export and adjust font substitution.
See Also
- Getting all used fonts in the loaded document
- Set custom fonts
- Replace missing font
- Exclude fonts when rendering to HTML
Get a free trial
You can download a free trial version of GroupDocs.Viewer for .NET from releases.groupdocs.com. You can also acquire a temporary license to try all features and functionalities without restrictions from here.