Unable to change the text default font when saving a TXT as Word/PDF/Jpeg

Hello,

When I try to convert a .txt file to Word/PDF/Jpeg the output file font its always “Times New Roman”. I tried several ways but the only way it work is using DocumentBuilder, but that isn’t possible because StreamReader have problemas detecting the file encode type.

So based on the documentation you provide in this like https://docs.aspose.com/words/net/manipulating-and-substitution-truetype-fonts/ here is an example of the code I tried:

public void GenerateImage(Stream file, string destPath)
{
    Aspose.Words.Fonts.FontSettings fontSettings = new Aspose.Words.Fonts.FontSettings();
    fontSettings.SubstitutionSettings.FontConfigSubstitution.Enabled = false;
    fontSettings.SubstitutionSettings.TableSubstitution.Enabled = false;
    fontSettings.SubstitutionSettings.FontInfoSubstitution.Enabled = false;
    fontSettings.SubstitutionSettings.DefaultFontSubstitution.Enabled = true; // If a default font was not defined "Times New Roman" will be used as default.
    fontSettings.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "Ariel"; // "Calibri"

    Aspose.Words.Loading.TxtLoadOptions txtLoadOptions = new Aspose.Words.Loading.TxtLoadOptions();
    txtLoadOptions.FontSettings = fontSettings;

    Aspose.Words.Document doc = new Aspose.Words.Document(file, txtLoadOptions);

    ImageSaveOptions imgOpt = new ImageSaveOptions(SaveFormat.Jpeg);
    imgOpt.PageSet = new PageSet(1);
    doc.Save(destfilename, imgOpt);
}

Our clients complain because the “Time New Roman” looks outdated and they want the file to look as similar as de default font type of the current most commonly used text file editors.
Can you check if this is a bug or is there a way to change the font to lets say Calibri?

Thanks

@duvidasAspose When you load document from TXT Aspose.Words applies Courier New font by default to the text. You can reset the font in the document loaded from TXT using the following simple code:

Document doc = new Document(@"C:\Temp\in.txt");

doc.Styles.DefaultFont.Name = "Arial";
foreach (Run r in doc.GetChildNodes(NodeType.Run, true))
    r.Font.ClearFormatting();

doc.Save(@"C:\Temp\out.pdf");

Thanks, the solution you provided solved the issue.

1 Like