TableSubstitutionRuleGetSubstitutes Method |
Namespace: Aspose.Words.Fonts
Document doc = new Document(); // Create a font settings object for our document doc.FontSettings = new FontSettings(); // By default we always start with a system font source Assert.AreEqual(1, doc.FontSettings.GetFontsSources().Length); SystemFontSource systemFontSource = (SystemFontSource)doc.FontSettings.GetFontsSources()[0]; Assert.AreEqual(FontSourceType.SystemFonts, systemFontSource.Type); Assert.AreEqual(0, systemFontSource.Priority); PlatformID pid = Environment.OSVersion.Platform; bool isWindows = (pid == PlatformID.Win32NT) || (pid == PlatformID.Win32S) || (pid == PlatformID.Win32Windows) || (pid == PlatformID.WinCE); if (isWindows) { const string fontsPath = @"C:\WINDOWS\Fonts"; Assert.AreEqual(fontsPath.ToLower(), SystemFontSource.GetSystemFontFolders().FirstOrDefault()?.ToLower()); } foreach (string systemFontFolder in SystemFontSource.GetSystemFontFolders()) { Console.WriteLine(systemFontFolder); } // Set a font that exists in the windows fonts directory as a substitute for one that doesn't doc.FontSettings.SubstitutionSettings.FontInfoSubstitution.Enabled = true; doc.FontSettings.SubstitutionSettings.TableSubstitution.AddSubstitutes("Kreon-Regular", new string[] { "Calibri" }); Assert.AreEqual(1, doc.FontSettings.SubstitutionSettings.TableSubstitution.GetSubstitutes("Kreon-Regular").Count()); Assert.Contains("Calibri", doc.FontSettings.SubstitutionSettings.TableSubstitution.GetSubstitutes("Kreon-Regular").ToArray()); // Alternatively, we could add a folder font source in which the corresponding folder contains the font FolderFontSource folderFontSource = new FolderFontSource(FontsDir, false); doc.FontSettings.SetFontsSources(new FontSourceBase[] { systemFontSource, folderFontSource }); Assert.AreEqual(2, doc.FontSettings.GetFontsSources().Length); // Resetting the font sources still leaves us with the system font source as well as our substitutes doc.FontSettings.ResetFontSources(); Assert.AreEqual(1, doc.FontSettings.GetFontsSources().Length); Assert.AreEqual(FontSourceType.SystemFonts, doc.FontSettings.GetFontsSources()[0].Type); Assert.AreEqual(1, doc.FontSettings.SubstitutionSettings.TableSubstitution.GetSubstitutes("Kreon-Regular").Count());
// Create a blank document and a new FontSettings object Document doc = new Document(); FontSettings fontSettings = new FontSettings(); doc.FontSettings = fontSettings; // Create a new table substitution rule and load the default Windows font substitution table TableSubstitutionRule tableSubstitutionRule = fontSettings.SubstitutionSettings.TableSubstitution; // If we select fonts exclusively from our own folder, we will need a custom substitution table FolderFontSource folderFontSource = new FolderFontSource(FontsDir, false); fontSettings.SetFontsSources(new FontSourceBase[] { folderFontSource }); // There are two ways of loading a substitution table from a file in the local file system // 1: Loading from a stream using (FileStream fileStream = new FileStream(MyDir + "Font substitution rules.xml", FileMode.Open)) { tableSubstitutionRule.Load(fileStream); } // 2: Load directly from file tableSubstitutionRule.Load(MyDir + "Font substitution rules.xml"); // Since we no longer have access to "Arial", our font table will first try substitute it with "Nonexistent Font", which we don't have, // and then with "Kreon", found in the "MyFonts" folder Assert.AreEqual(new[] { "Missing Font", "Kreon" }, tableSubstitutionRule.GetSubstitutes("Arial").ToArray()); // If we find this substitution table lacking, we can also expand it programmatically // In this case, we add an entry that substitutes "Times New Roman" with "Arvo" Assert.Null(tableSubstitutionRule.GetSubstitutes("Times New Roman")); tableSubstitutionRule.AddSubstitutes("Times New Roman", "Arvo"); Assert.AreEqual(new[] { "Arvo" }, tableSubstitutionRule.GetSubstitutes("Times New Roman").ToArray()); // We can add a secondary fallback substitute for an existing font entry with AddSubstitutes() // In case "Arvo" is unavailable, our table will look for "M+ 2m" tableSubstitutionRule.AddSubstitutes("Times New Roman", "M+ 2m"); Assert.AreEqual(new[] { "Arvo", "M+ 2m" }, tableSubstitutionRule.GetSubstitutes("Times New Roman").ToArray()); // SetSubstitutes() can set a new list of substitute fonts for a font tableSubstitutionRule.SetSubstitutes("Times New Roman", new[] { "Squarish Sans CT", "M+ 2m" }); Assert.AreEqual(new[] { "Squarish Sans CT", "M+ 2m" }, tableSubstitutionRule.GetSubstitutes("Times New Roman").ToArray()); // TO demonstrate substitution, write text in fonts we have no access to and render the result in a PDF DocumentBuilder builder = new DocumentBuilder(doc); builder.Font.Name = "Arial"; builder.Writeln("Text written in Arial, to be substituted by Kreon."); builder.Font.Name = "Times New Roman"; builder.Writeln("Text written in Times New Roman, to be substituted by Squarish Sans CT."); doc.Save(ArtifactsDir + "Font.TableSubstitutionRule.Custom.pdf");