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

你离开我真会死。 提交于 2019-12-04 13:42:00

系统中明明有字体的,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);

  

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!