Enable Disable Font Substitution

In this tutorial, we will walk you through how to enable or disable font substitution in a Word document when rendering it using the Aspose.Words library for .NET. Enabling or disabling font substitution allows you to control whether missing fonts are automatically replaced with a default font. We’ll take you step-by-step to help you understand and implement the code in your .NET project.

Prerequisites

Before you begin, make sure you have the following items:

  • A working knowledge of the C# programming language
  • The Aspose.Words library for .NET installed in your project
  • A Word document that you want to render with or without font substitution

Step 1: Define the document directory

First, you need to set the directory path to the location of your Word document. Replace "YOUR DOCUMENT DIRECTORY" in the code with the appropriate path.

// Path to your documents directory
string dataDir = "YOUR DOCUMENTS DIRECTORY";

Step 2: Upload the document and configure the font settings

Next, we’ll load the Word document you want to render and create an instance of the FontSettings class to handle the font settings. We’ll set the default font override by specifying the font name in DefaultFontName and disable font information override with Enabled set to false.

// Load the document
Document doc = new Document(dataDir + "Rendering.docx");

// Configure font settings
FontSettings fontSettings = new FontSettings();
fontSettings.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "Arial";
fontSettings.SubstitutionSettings.FontInfoSubstitution.Enabled = false;

// Apply the font settings to the document
doc.FontSettings = fontSettings;

Step 3: Save the rendered document

Finally, we’ll save the rendered document, which will respect the defined font override settings.

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

Sample source code for Enable Disable Font Substitution using Aspose.Words for .NET


// Path to your document directory 
string dataDir = "YOUR DOCUMENT DIRECTORY";

Document doc = new Document(dataDir + "Rendering.docx");
FontSettings fontSettings = new FontSettings();
fontSettings.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "Arial";
fontSettings.SubstitutionSettings.FontInfoSubstitution.Enabled = false;
doc.FontSettings = fontSettings;
doc.Save(dataDir + "WorkingWithFonts.EnableDisableFontSubstitution.pdf");

Conclusion

In this tutorial, we saw how to enable or disable font substitution in a Word document when rendering it with Aspose.Words for .NET. By controlling font substitution, you can influence how missing fonts are handled in your rendered documents. Do not hesitate to use this feature to customize the management of fonts in your Word documents.

FAQ’s

Q: How can I enable font substitution in a Word document with Aspose.Words?

A: To enable font substitution in a Word document with Aspose.Words, you can use the API to specify substitution fonts to use when required fonts are not available. This will ensure consistent text visualization, even without the original fonts.

Q: Is it possible to disable font substitution in a Word document with Aspose.Words?

A: Yes, with Aspose.Words you can disable font substitution in a Word document. By using the API, you can prevent Word from substituting required fonts with other fonts, which maintains the original appearance of the text.

Q: What happens when required fonts are missing during substitution in a Word document?

A: When required fonts are missing during substitution in a Word document, Aspose.Words can detect this issue and provide you with options to fix it. You can choose to substitute missing fonts with alternate fonts or include missing fonts in the document, ensuring correct viewing.

Q: How can I handle missing fonts when substituting in a Word document with Aspose.Words?

A: To handle missing fonts when substituting in a Word document with Aspose.Words, you can use the API to detect missing fonts and provide resolution options. You can choose to substitute missing fonts with alternative fonts or include missing fonts in the document, depending on your needs.

Q: Is it important to control font substitution in a Word document?

A: Yes, it is important to control font substitution in a Word document to maintain the visual integrity of the text. By using Aspose.Words to enable or disable font substitution, you can ensure that the required fonts are used and avoid problems with missing or substituted fonts.