Net Core3.1使用Aspose.Words18.4将Word转PDF

最近做个文件在线预览的需求,在线预览Word、Excel、PDF等文件。Word一直使用的是ce.office.extension将文件转为html预览,有时候存在格式、图片、字体错乱的问题。

转换思路,使用Aspose.Words18.4将服务器存储的Word转换为PDF,再使用PDF.js 进行预览。 

网上找了好多例子,大部分都是骗子,尤其C、S、D、N骗积分的太多,这个版本支持netcore3.1,再高的无限制版暂未找到,此版本可以正常使用。Aspose.Words18.4无限制版链接:baidu网盘提取码:h6hf

baidu网盘pan.baidu.com/s/1ikgAKYwkAWSwKQZX6MvE2g

代码如下:

string FilePath = "Word文件的路径,包括文件名";
string FilePathPdf = "生成PDF文件的路径,包括文件名";


//处理字体-取用户字体目录
string userfontsfoloder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Microsoft\\Windows\\Fonts\\";
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);
//转换PDF文件
Aspose.Words.Document doc = new Aspose.Words.Document(FilePath);
if (doc != null)
{
	doc.Save(FilePathPdf, Aspose.Words.SaveFormat.Pdf);
}

Ps:处理字体文件原因:有些后期安装的非系统自带字体转换时不对,比如说:小标宋。读取系统安装字体,覆盖在字体目录中。

PDF生成成功后,再使用PDF.js 前端进行预览。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值