zoukankan      html  css  js  c++  java
  • C#将Word转换成PDF方法总结(基于Office和WPS两种方案)

      有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片。一个比较好的办法就是将word转换成pdf,然后让客户预览,下面来看一下基于Office和WPS的两种解决方案。

     一、基于Office的解决方案

      正如标题所说,基于Office就是要求服务器上面要安装的有Office。我们通过C#代码来调用COM接口,实现将Word转换成PDF。下面来看一下具体实现,首先引用Microsoft.Office.Interop.Word.dll,然后编写如下代码:

    public bool WordToPDF(string sourcePath, string targetPath)
            {
               bool result = false;
               Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
               Document document = null;
               try
               {
                  application.Visible = false;
                  document = application.Documents.Open(sourcePath);
                  document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
                  result = true;
               }
               catch (Exception e)
               {
                  Console.WriteLine(e.Message);
                  result = false;
               }
               finally
               {
                  document.Close();
               }
               return result;
            }

     ExportAsFixedFormat方法的参数如下:

    参考连接:https://msdn.microsoft.com/library/microsoft.office.tools.word.document.exportasfixedformat(v=vs.100).aspx

     二、基于WPS的解决方案

       WPS最大的好处当然是免费,还有就是体积小。要实现Word到PDF的转换,当然,这个要求服务器上面必须安装的有WPS,我们调用的仍然是COM接口,然后编写如下代码:

    public bool WordToPdfWithWPS(string sourcePath, string targetPath)
            {
                WPS.ApplicationClass app = new WPS.ApplicationClass();
                WPS.Document doc = null;
               try
               {
                  doc = app.Documents.Open(sourcePath, true, true, false, null, null, false, "", null, 100, 0, true, true, 0, true);
                  doc.ExportPdf(targetPath, "", "");
               }
               catch (Exception ex)
               {
                  Console.WriteLine(ex.Message);
                  return false;
               }
               finally
               {
                  doc.Close();
               }
               return true;
            }
     三、企业级解决方案

       对于大型企业来说,往往有多台服务器,不可能在每台服务器上面安装office或者会WPS,或者说公司根本不想在服务器上面安装这些没用的软件。这个时候该怎么办呢?毕竟服务器上面安装这些软件就是一种资源的浪费。

      当然,功能还是要实现的,那么该怎么解决呢?实际上,我们可以在一台服务器上面安装ofiice或者WPS软件,然后部署WCF服务或者remoting等WebService,其他的服务器可以调用这个服务来实现Word到PDF的转换。

     作者:雲霏霏

    QQ交流群:243633526

     博客地址:http://www.cnblogs.com/yunfeifei/

     声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未授权,贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文连接。

    如果大家感觉我的博文对大家有帮助,请推荐支持一把,给我写作的动力。

  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    How to check if one path is a child of another path?
    Why there is two completely different version of Reverse for List and IEnumerable?
    在Jenkins中集成Sonarqube
    如何查看sonarqube的版本 how to check the version of sonarqube
    Queue
    BFS广度优先 vs DFS深度优先 for Binary Tree
    Depth-first search and Breadth-first search 深度优先搜索和广度优先搜索
    102. Binary Tree Level Order Traversal 广度优先遍历
    How do I check if a type is a subtype OR the type of an object?
  • 原文地址:https://www.cnblogs.com/yunfeifei/p/4520414.html
Copyright © 2011-2022 走看看