600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 转换Word文档为PDF文件

转换Word文档为PDF文件

时间:2023-09-12 06:20:53

相关推荐

转换Word文档为PDF文件

1.使用 Office COM组件的Microsoft.Office.Interop.word.dll库

该方法需要在电脑上安装Office软件,并且需要Office支持转换为PDF格式,如果不支持,从官网下载一个SaveAsPDFandXPS.exe插件

Interop.word程序集可以通过Nuget程序包获取,实现代码如下:

public bool WordToPDF2(string sourcePath){bool result = false;Word.Application application = new Word.Application();Word.Document document = null;try{application.Visible = false;document = application.Documents.Open(sourcePath);string PDFPath = sourcePath.Replace(".doc", ".pdf");//pdf存放位置if (!File.Exists(PDFPath))//存在PDF,不需要继续转换{document.ExportAsFixedFormat(PDFPath, Word.WdExportFormat.wdExportFormatPDF);}result = true;}catch (Exception e){Console.WriteLine(e.Message);result = false;}finally{document.Close();}return result;}

2.使用Aspose.Words组件

首先需要引用Aspose.Words.dll,链接地址:/s/1rJvjp-kMsEterYf_oud28Q 提取码:awiw

代码如下:

public bool WordToPDF1(string sourcePath){try{Document doc = new Document(sourcePath);string targetPath = sourcePath.ToUpper().Replace(".DOCX", ".PDF");doc.Save(targetPath,SaveFormat.Pdf);}catch(Exception e){Console.WriteLine(e.Message);return false;}return true;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。