Doc to PDF DefaultFontName not Working

Description

The font on doc is not installed on my computer, but after converting to PDF, it is not the default font I set, but the first font of all fonts on my computer.

Env

  • Windows 11
  • Aspose.Words for .NET 24.1

Code

void Test()
{
	Aspose.Words.Fonts.FontSettings.DefaultInstance.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "Arial Unicode MS";
	var doc = new Aspose.Words.Document(@"C:\Users\Administrator\Desktop\test.doc");
	var ops = new Aspose.Words.Saving.PdfSaveOptions();
	doc.Save(@"C:\Users\Administrator\Desktop\test.pdf", ops);
}

Files



1.zip (79.1 KB)

@xucongli1989 Could you please attach your input and output document here for testing? We will check the issue and provide you more information. What fonts are available in your environment.

DefaultFontSubstitution is penultimate substitution rule applied. To force it to be applied you should disable other substitution rules. The first available font substitution rule is applied after DefaultFontSubstitution rule, so most likely the default font you have specified is not available:
https://docs.aspose.com/words/net/manipulating-and-substitution-truetype-fonts/#font-availability-and-substitution

Sorry, I had attached text file above. Thanks.

void Test()
{

	Aspose.Words.Fonts.FontSettings.DefaultInstance.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "微软雅黑";
	var doc = new Aspose.Words.Document(@"C:\Users\Administrator\Desktop\test.doc");
	doc.WarningCallback = new DocumentSubstitutionWarnings();
	var ops = new Aspose.Words.Saving.PdfSaveOptions();
	doc.Save(@"C:\Users\Administrator\Desktop\test.pdf", ops);
}


public class DocumentSubstitutionWarnings : IWarningCallback
{
	public void Warning(WarningInfo info)
	{
		info.Dump();
	}
	public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}

@xucongli1989 As I can see by the provided warnings the fonts are substituted before default font substation rule is applied. There are substituted with font info substitution and alternative name substitution, which are applied before default font substitution rule.

This all seems a bit complicated, I just want that when converting word to pdf, if the font does not exist, a default font should be set. Currently this default font is the first among all Chinese fonts installed on my computer. I want to change it. I don’t want it to take the first Chinese font, but the font name I set myself. So, how should I modify this code?

It seems like I should modify it like this?

	doc.FontInfos.ToList().ForEach(k => {
		k.AltName="Microsoft YaHei UI";
	});

@xucongli1989 What is your ultimate goal? Do you need to change font of the whole document before converting it to PDF?

I don’t need to modify the doc, I just convert the doc to pdf, that’s it. Also, I’d like to know how this font is calculated:

Code


void Test()
{
	//Aspose.Words.Fonts.FontSettings.DefaultInstance.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName ="Times New Roman";
	var doc = new Aspose.Words.Document(@"C:\Users\Administrator\Desktop\test.doc");
	doc.FontInfos.Dump();
	doc.WarningCallback = new DocumentSubstitutionWarnings();
	var ops = new Aspose.Words.Saving.PdfSaveOptions();
	doc.Save(@"C:\Users\Administrator\Desktop\test.pdf", ops);
}


public class DocumentSubstitutionWarnings : IWarningCallback
{
	public void Warning(WarningInfo info)
	{
		info.Dump();
	}
	public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}

Summary

I’d rather it be Microsoft YaHei UI than zihun55hao-longyinshoushu, so I want to change it.
image.png (266.0 KB)

@xucongli1989 In the font substitution warning there is a reason. In this case this font info substitution rule. So the substitution is determined by the information from FontInfo stored in the document. Aspose.Words takes in account font panose, unicode ranges, codepages, pitch, charset and weight of the font to select a suitable substitution.

I changed code,and it’s ok for me. Thanks.

void Test()
{
	Aspose.Words.Fonts.FontSettings.DefaultInstance.SubstitutionSettings.FontInfoSubstitution.Enabled = false;// Important!!!!!
	Aspose.Words.Fonts.FontSettings.DefaultInstance.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "Microsoft YaHei UI";
	Aspose.Words.Fonts.FontSettings.DefaultInstance.SubstitutionSettings.DefaultFontSubstitution.Enabled = true;
	var doc = new Aspose.Words.Document(@"C:\Users\Administrator\Desktop\test.doc");
	doc.WarningCallback = new DocumentSubstitutionWarnings();
	var ops = new Aspose.Words.Saving.PdfSaveOptions();
	doc.Save(@"C:\Users\Administrator\Desktop\test.pdf", ops);
}


public class DocumentSubstitutionWarnings : IWarningCallback
{
	public void Warning(WarningInfo info)
	{
		info.Dump();
	}
	public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}
1 Like

@xucongli1989 It is perfect that you managed to resolve the problem.