Aspose.Words转换为PDF的时候字体丢失的问题解决

系统中明明有字体的,Word中显示也正常,就是转换为PDF以后不正常,字体丢失,被替换成了等线字体

好一番研究,终于找到原因

,原因是WindowsFonts下的文件,有些只是虚拟的路径,真正的字体文件是在C:Users用户名AppDataLocalMicrosoftWindowsFonts 这个目录下,从而导致的这个问题

只要将用户的字体目录添加进去就可以了,代码如下

//取用户字体目录
            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);

  

原文地址:https://www.cnblogs.com/icejd/p/11868691.html