当前位置: 移动技术网 > IT编程>开发语言>.net > 转换Word文档为PDF文件

转换Word文档为PDF文件

2018年11月08日  | 移动技术网IT编程  | 我要评论

搜房网666装修,婴儿奶粉品牌排行榜,痞子英雄迅雷下载

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,链接地址:https://pan.baidu.com/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;
 }

 

  

 

 

 

 

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网