Set Font Fallback Settings

When working with documents that contain diverse text elements, such as different languages or special characters, it’s crucial to ensure that these elements are displayed correctly. Aspose.Words for .NET offers a powerful feature called Font Fallback Settings, which helps in defining rules for substituting fonts when the original font doesn’t support certain characters. In this guide, we’ll explore how to set up Font Fallback Settings using Aspose.Words for .NET in a step-by-step tutorial.

Prerequisites

Before diving into the tutorial, make sure you have the following prerequisites in place:

  • Basic Knowledge of C#: Familiarity with C# programming language and .NET framework.
  • Aspose.Words for .NET: Download and install from the download link.
  • Development Environment: A setup like Visual Studio to write and run your code.
  • Sample Document: Have a sample document (e.g., Rendering.docx) ready for testing.
  • Font Fallback Rules XML: Prepare an XML file defining the font fallback rules.

Import Namespaces

To use Aspose.Words, you need to import the necessary namespaces. This allows access to various classes and methods required for document processing.

using Aspose.Words;
using Aspose.Words.Fonts;
using System;

Step 1: Define the Document Directory

First, define the directory where your document is stored. This is essential for locating and processing your document.

// The path to the documents directory
string dataDir = "YOUR DOCUMENT DIRECTORY";

Step 2: Load the Document

Load your document into an Aspose.Words Document object. This step allows you to work with the document programmatically.

Document doc = new Document(dataDir + "Rendering.docx");

Step 3: Configure Font Settings

Create a new FontSettings object and load the font fallback settings from an XML file. This XML file contains the rules for font fallback.

FontSettings fontSettings = new FontSettings();
fontSettings.FallbackSettings.Load(dataDir + "Font fallback rules.xml");

Step 4: Apply Font Settings to the Document

Assign the configured FontSettings to the document. This ensures that the font fallback rules are applied when rendering the document.

doc.FontSettings = fontSettings;

Step 5: Save the Document

Finally, save the document. The font fallback settings will be used during the save operation to ensure proper font substitution.

doc.Save(dataDir + "WorkingWithFonts.SetFontFallbackSettings.pdf");

XML File: Font Fallback Rules

Here is an example of how your XML file defining the font fallback rules should look:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FontFallbackSettings xmlns="Aspose.Words">
    <FallbackTable>
        <Rule Ranges="0B80-0BFF" FallbackFonts="Vijaya"/>
        <Rule Ranges="1F300-1F64F" FallbackFonts="Segoe UI Emoji, Segoe UI Symbol"/>
        <Rule Ranges="2000-206F, 2070-209F, 20B9" FallbackFonts="Arial" />
        <Rule Ranges="3040-309F" FallbackFonts="MS Gothic" BaseFonts="Times New Roman"/>
        <Rule Ranges="3040-309F" FallbackFonts="MS Mincho"/>
        <Rule FallbackFonts="Arial Unicode MS"/>
    </FallbackTable>
</FontFallbackSettings>

Conclusion

By following these steps, you can effectively set up and use Font Fallback Settings in Aspose.Words for .NET. This ensures that your documents display all characters correctly, even if the original font does not support certain characters. Implementing these settings will greatly enhance the quality and readability of your documents.

FAQs

Q1: What is Font Fallback?

Font Fallback is a feature that allows the substitution of fonts when the original font doesn’t support certain characters, ensuring proper display of all text elements.

Q2: Can I specify multiple fallback fonts?

Yes, you can specify multiple fallback fonts in the XML rules. Aspose.Words will check each font in the order specified until it finds one that supports the character.

Q3: Where can I download Aspose.Words for .NET?

You can download it from the Aspose download page.

Q4: How do I create the XML file for font fallback rules?

The XML file can be created using any text editor. It should follow the structure shown in the example provided in this tutorial.

Q5: Is there support available for Aspose.Words?

Yes, you can find support on the Aspose.Words support forum.