系統中明明有字體的,Word中顯示也正常,就是轉換為PDF以后不正常,字體丟失,被替換成了等線字體
好一番研究,終於找到原因
,原因是Windows\Fonts下的文件,有些只是虛擬的路徑,真正的字體文件是在C:\Users\用戶名AppData\Local\Microsoft\Windows\Fonts\ 這個目錄下,從而導致的這個問題
只要將用戶的字體目錄添加進去就可以了,代碼如下
//取用戶字體目錄
string userfontsfoloder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+ "\\Microsoft\\Windows\\Fonts\\";
Console.WriteLine("userfontsfoloder:" + userfontsfoloder);
string outFilePdf = Path.GetDirectoryName(txtpicDir.Text) + "\\" + Path.GetFileNameWithoutExtension(txtpicDir.Text) + ".pdf";
Aspose.Words.Document document = new Aspose.Words.Document(txtpicDir.Text);
ArrayList fontSources = new ArrayList(FontSettings.DefaultInstance.GetFontsSources());
//將用戶目錄字體添加到字體源中
Aspose.Words.Fonts.FolderFontSource folderFontSource = new Aspose.Words.Fonts.FolderFontSource(userfontsfoloder, true);
fontSources.Add(folderFontSource);
Aspose.Words.Fonts.FontSourceBase[] updatedFontSources = (Aspose.Words.Fonts.FontSourceBase[])fontSources.ToArray(typeof(Aspose.Words.Fonts.FontSourceBase));
FontSettings.DefaultInstance.SetFontsSources(updatedFontSources);
