当前位置: 代码迷 >> C# >> 关于导出PDF文件的有关问题
  详细解决方案

关于导出PDF文件的有关问题

热度:40   发布时间:2016-05-05 04:09:39.0
关于导出PDF文件的问题
最近负责一个项目,接到个任务就是在页面上通过“导出”按钮能够提供一个链接,并且下载的是PDF文件
因为这个页面是通过后台html拼接组成的页面,显示的是报告文件,而原本”导出“按钮出现的下载链接是可以下载word文件的
而我也在网上搜寻了些方法
一种是直接将页面导出生成PDF文件,另外种是通过已经生成的word文件转换为pdf文件,大家觉得哪种方法比较好
有没有这两种的例子能够参考,谢谢
------解决思路----------------------
调用方法:
相对路径和绝对路径都试过都可以生成转换的

WordToPdf("E:\\D201432010041999_yolanda test.doc", "E:\\D201432010041999_yolanda test.pdf");


下面是方法:

  /// <summary>
        /// 把Word文件转换成pdf文件
        /// </summary>
        /// <param name="sourcePath">需要转换的文件路径和文件名称</param>
        /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
        /// <returns>成功返回true,失败返回false</returns>
        public static bool WordToPdf(object sourcePath, string targetPath) //将word转为pdf文件的方法
        {
            bool result = false;
            Microsoft.Office.Interop.Word.WdExportFormat wdExportFormatPDF = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
            object missing = Type.Missing;
            Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
            Microsoft.Office.Interop.Word.Document document = null;
            try
            {
                applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
                document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                if (document != null)
                {
                    document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint, Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 0, 0, Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent, true, true, Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (document != null)
                {
                    document.Close(ref missing, ref missing, ref missing);
                    document = null;
                }
                if (applicationClass != null)
                {
                    applicationClass.Quit(ref missing, ref missing, ref missing);
                    applicationClass = null;
                }
            }
            return result;
        }


记得还要引用组件,名字是"Microsoft Word 14.0 Object Library"
试过了可以的从word转pdf
  相关解决方案