码迷,mamicode.com
首页 > 其他好文 > 详细

word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览

时间:2017-10-18 11:14:42      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:img   fadeout   xhtml   rom   match   standard   round   dal   strong   

  • 自己上网查了好多种方法,最后还是选了这种不收费,还挺好用的方法

           为了用户有一个好的体验我将word、excel、ppt转Pdf,Pdf转Swf写在服务上,因为我当时做的时候Pdf转Swf会执行pdf2swf.exe弹出黑框,对用户体验不好,还有就是ppt转swf时会有一个弹出框,提示正在发布,我没搞明白,就把他们都写在服务上了。

  • Office2Pdf
 /// <summary>
    /// Office2Pdf 将Office文档转化为pdf
    /// </summary>
    public class Office2PDFHelper
    {
        public Office2PDFHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        #region 1.0 Word转换成pdf + bool DOCConvertToPDF(string sourcePath, string targetPath)
        /// <summary>
        /// Word转换成pdf
        /// </summary>
        /// <param name="sourcePath">源文件路径(物理路径)</param>
        /// <param name="targetPath">目标文件路径(物理路径)</param>
        /// <returns>true=转换成功</returns>
        public static bool DOCConvertToPDF(string sourcePath, string targetPath)
        {
        //    string targetPath = "G:\\工作\\ceshi\\ceshi.pdf";
        //    sourcePath = "G:\\工作\\OfficePreview\\Preview\\SourceFile\\测试.doc"; 
            bool result = false;
            Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
            object paramMissing = Type.Missing;
            Word.ApplicationClass wordApplication = new Word.ApplicationClass();
            Word._Document wordDocument = null;
            try
            {
                object paramSourceDocPath = sourcePath;
                string paramExportFilePath = targetPath;
                Word.WdExportFormat paramExportFormat = exportFormat;
                bool paramOpenAfterExport = false;
                Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;
                wordDocument = wordApplication.Documents.Open(
                    ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing);
                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        } 
        #endregion

        #region 2.0 把Excel文件转换成PDF格式文件 + 把Excel文件转换成PDF格式文件  
        /// <summary>
        /// 把Excel文件转换成PDF格式文件  
        /// </summary>
        /// <param name="sourcePath">源文件路径(物理路径)</param>
        /// <param name="targetPath">目标文件路径(物理路径)</param>
        /// <returns>true=转换成功</returns>
        public static bool XLSConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
            object missing = Type.Missing;
            Excel.ApplicationClass application = null;
            Excel.Workbook workBook = null;
            try
            {
                application = new Excel.ApplicationClass();
                object target = targetPath;
                object type = targetType;
                workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, missing, missing);
                    workBook = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        } 
        #endregion

        #region 3.0 把PowerPoint文件转换成PDF格式文件   + bool PPTConvertToPDF(string sourcePath, string targetPath)
        ///<summary>        
        /// 把PowerPoint文件转换成PDF格式文件       
        ///</summary>        
        ///<param name="sourcePath">源文件路径(物理路径)</param>     
        ///<param name="targetPath">目标文件路径(物理路径)</param> 
        ///<returns>true=转换成功</returns> 
        public static bool PPTConvertToPDF(string sourcePath, string targetPath)
        {
            bool result;
            PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
            object missing = Type.Missing;
            PowerPoint.ApplicationClass application = null;
            PowerPoint.Presentation persentation = null;
            try
            {
                application = new PowerPoint.ApplicationClass();
                //打开
                persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                if (persentation != null)
                {
                    //写入
                    persentation.SaveAs(targetPath, targetFileType, MsoTriState.msoTrue);
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        } 
        #endregion


    }
  • PDF2SwfHelper
 1         #region 1.0 转换所有的页,图片质量100% +  bool PDF2SWF(string pdfPath, string swfPath)
 2         /// <summary>
 3         /// 转换所有的页,图片质量100%
 4         /// </summary>
 5         /// <param name="pdfPath">PDF文件地址</param>
 6         /// <param name="swfPath">生成后的SWF文件地址</param>
 7         public static bool PDF2SWF(string pdfPath, string swfPath)
 8         {
 9             return PDF2SWF(pdfPath, swfPath, 1, GetPageCount(pdfPath), 100);
10         } 
11         #endregion
12 
13         #region 2.0 转换前N页,图片质量100% + bool PDF2SWF(string pdfPath, string swfPath, int page)
14         /// <summary>
15         /// 转换前N页,图片质量100%
16         /// </summary>
17         /// <param name="pdfPath">PDF文件地址</param>
18         /// <param name="swfPath">生成后的SWF文件地址</param>
19         /// <param name="page">页数</param>
20         public static bool PDF2SWF(string pdfPath, string swfPath, int page)
21         {
22             return PDF2SWF(pdfPath, swfPath, 1, page, 100);
23         } 
24         #endregion
25 
26         #region 3.0 PDF格式转为SWF + bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
27         /// <summary>
28         /// PDF格式转为SWF
29         /// </summary>
30         /// <param name="pdfPath">PDF文件地址</param>
31         /// <param name="swfPath">生成后的SWF文件地址</param>
32         /// <param name="beginpage">转换开始页</param>
33         /// <param name="endpage">转换结束页</param>
34         private static bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
35         {
36             
37             //swftool,首先先安装,然后将安装目录下的东西拷贝到相应目录下
38             string exe = System.IO.Path.GetFullPath("pdf2swf.exe");
39             //string exe = System.Web.Hosting.HostingEnvironment.MapPath("~");
40             //pdfPath = HttpContext.Current.Server.MapPath(pdfPath);
41             //swfPath = HttpContext.Current.Server.MapPath(swfPath);
42             if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath))
43             {
44                 return false;
45             }
46             StringBuilder sb = new StringBuilder();
47             sb.Append(" \"" + pdfPath + "\"");
48             sb.Append(" -o \"" + swfPath + "\"");
49             sb.Append(" -s flashversion=9");
50             if (endpage > GetPageCount(pdfPath)) endpage = GetPageCount(pdfPath);
51             sb.Append(" -p " + "\"" + beginpage + "" + "-" + endpage + "\"");
52             sb.Append(" -j " + photoQuality);
53             string Command = sb.ToString();
54             System.Diagnostics.Process p = new System.Diagnostics.Process();
55             p.StartInfo.FileName = exe;
56             p.StartInfo.Arguments = Command;
57             p.StartInfo.WorkingDirectory = System.IO.Path.GetFullPath("../Release/");
58             string ss = p.StartInfo.WorkingDirectory;
59             p.StartInfo.UseShellExecute = false;
60             p.StartInfo.RedirectStandardError = true;
61             p.StartInfo.CreateNoWindow = false;
62             p.Start();
63             p.BeginErrorReadLine();
64             p.WaitForExit();
65             p.Close();
66             p.Dispose();
67             return true;
68         }
69         
70         #endregion
71 
72         #region 4.0 返回页数 + int GetPageCount(string pdfPath)
73         /// <summary>
74         /// 返回页数
75         /// </summary>
76         /// <param name="pdfPath">PDF文件地址</param>
77         private static int GetPageCount(string pdfPath)
78         {
79             byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
80             int length = buffer.Length;
81             if (buffer == null)
82                 return -1;
83             if (buffer.Length <= 0)
84                 return -1;
85             string pdfText = Encoding.Default.GetString(buffer);
86             System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
87             System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
88             return matches.Count;
89         } 
90         #endregion
  • flexpaper+swftools
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OfficePreview.aspx.cs" Inherits="Preview.OfficePreview" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script src="js/swfPreview/jquery.js" type="text/javascript"></script>
    <script src="js/swfPreview/flexpaper_flash_debug.js" type="text/javascript"></script>
    <script src="js/swfPreview/flexpaper_flash.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var winWidth = 0;
            var winHeight = 0;
            function findDimensions() //函数:获取尺寸 
            {
                ////获取窗口宽度 
                //if (window.innerWidth)
                //    winWidth = window.innerWidth;
                //else if ((document.body) && (document.body.clientWidth))
                //    winWidth = document.body.clientWidth;

                //获取窗口高度 
                if (window.innerHeight)
                    winHeight = window.innerHeight;
                else if ((document.body) && (document.body.clientHeight))
                    winHeight = document.body.clientHeight;
                //通过深入Document内部对body进行检测,获取窗口大小 && document.documentElement.clientWidth
                if (document.documentElement && document.documentElement.clientHeight) {
                    winHeight = document.documentElement.clientHeight;
                    //winWidth = document.documentElement.clientWidth;
                }
                //高度
                $("#viewerPlaceHolder").height(winHeight);
            }
            findDimensions();
            //调用函数,获取数值 
            window.onresize = findDimensions;
        })
    </script>
    <style type="text/css">
        body
        {
            margin-left: 0px;
            margin-top: 0px;
            margin-bottom: 0px;
            margin-right: 0px;
            height: 100%;
        }

        #div1
        {
            margin: 0px auto;
            /*border: 0px solid #0094ff;*/
            width: 100%;
            height: 100%;
        }

        #viewerPlaceHolder
        {
            /*height: 598px;*/
        }
    </style>
</head>
<body style="overflow-y: hidden">
    <div id="center">
        <a id="viewerPlaceHolder" style="display: block;"></a>
        <script type="text/javascript">
            $(function () {
                $("#pageloading").show();
                var fp = new FlexPaperViewer(
       ‘js/swfPreview/FlexPaperViewer‘,
       ‘viewerPlaceHolder‘,
       {
           config: {
               SwfFile: escape(‘TargetFile/Swf/swf.swf‘),//这里是要显示的swf的位置,相对根目录
               Scale: 1,//缩放比例
               ZoomTransition: ‘easeOut‘,//Flexpaper中缩放样式,它使用和Tweener一样的样式,默认参数值为easeOut.其他可选值包括: easenone, easeout, linear, easeoutquad
               ZoomTime: 0.5,//从一个缩放比例变为另外一个缩放比例需要花费的时间,该参数值应该为0或更大。
               ZoomInterval: 0.2,//缩放比例之间间隔,默认值为0.1,该值为正数。
               FitPageOnLoad: false,//初始化的时候自适应页面,与使用工具栏上的适应页面按钮同样的效果。
               FitWidthOnLoad: false,//初始化的时候自适应页面宽度,与工具栏上的适应宽度按钮同样的效果。
               PrintEnabled: false,//是否支持打印
               FullScreenAsMaxWindow: false,//是否支持打印
               ProgressiveLoading: false,//当设置为true的时候,展示文档时不会加载完整个文档,而是逐步加载,但是需要将文档转化为9以上的flash版本(使用pdf2swf的时候使用-T 9 标签)。
               MinZoomSize: 0.2,//最小的缩放比例。
               MaxZoomSize: 5,//设置最大的缩放比例。
               SearchMatchAll: false,//设置为true的时候,单击搜索所有符合条件的地方高亮显示。
               InitViewMode: ‘TwoPage‘,//启动模式,如”Portrait” or “TwoPage”.
               ViewModeToolsVisible: false,//工具栏上是否显示样式选择框(就是显示缩略图或分页显示的工具)
               ZoomToolsVisible: true,//工具栏上是否显示缩放工具
               NavToolsVisible: false,//工具栏上是否显示导航工具(也就是页码工具)
               CursorToolsVisible: true,//工具栏上是否显示光标工具
               SearchToolsVisible: true,//工具栏上是否显示搜索
               localeChain: ‘zh_CN‘//语言
           }
       }
       );
                $("#pageloading").fadeOut(1000);
            });
        </script>
    </div>
    <div id="pageloading" style="background-color: White; width: 100%; height: 100%; position: fixed; top: 0; left: 0; display: none; text-align: center; margin: 0px auto; vertical-align: middle; z-index: 999">
        <img src="/images/loading.gif" style="width: 48px; height: 48px; position: fixed; top: 45%; left: 45%; right: 50%; bottom: 50%;" />
    </div>
    <div id="txtPreview" style="width: 100%; height: 100%; top: 0; left: 0;">
        <pre id="pre"></pre>
    </div>
</body>
</html>

 

word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览

标签:img   fadeout   xhtml   rom   match   standard   round   dal   strong   

原文地址:http://www.cnblogs.com/FreedomAndLove/p/4481079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!