现在的位置: 首页 > 综合 > 正文

我在设计在线文库中的一些小记(在线文库系列二之文档转换)

2011年07月10日 ⁄ 综合 ⁄ 共 5253字 ⁄ 字号 评论关闭

大家好,即上一篇讲述在线文档的思路后,我已经实现了在线文档的初版,但在文档加载和预装载方面有小小的性能问题,目前正在优化。

小提示:(由于项目的保密性,我把转换的业务核心写给大家,至于我们的业务流程,就不大罗列了,多半你也用不着,或者压根就没用。)

入题:

一、由doc、docx、xls等等文档到pdf的转换过程。

        由doc、docx、xls等等文档到pdf的转换过程我是借助FlashPaper完成的,所以在要完成这个操作,大家必须安装flashpaper,至于flashpaper的版本吗!您就自己斟酌吧!理念是,能用就行,好用即可。

    /// <summary>
        /// 将用户所上传文件转化成为pdf文件
        /// </summary>
        private void ConvertToPdf(string resFilePath, string pdfFilePath)
        {
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

                p.Start();
                string strOutput = null;
                string s = ConfigurationSettings.AppSettings["FlashPaper"] + resFilePath + " -o " + pdfFilePath;

                p.StandardInput.WriteLine(s);
                p.StandardInput.WriteLine("exit");
                strOutput = p.StandardOutput.ReadToEnd();

                Console.WriteLine(strOutput);
                p.WaitForExit();
                p.Close();
            }
            catch (Exception ex)
            {
                LogHelper.Info("转化成为pdf时出发生错误" + ex.Message);
                throw ex;
            }
        }

二、完成了向pdf的转换过程、接下来我们实现向swf的转换。

      pdf到swf的转换过程我是借用的SWFTools系列工具之pdf2swf.exe,至于下载地址,您Google即可。

   

        /// <summary>
        /// 将用户所上传文件转化成为swf文件

        ///pdfFilePath,就是要转换的pdf文件路径
        /// </summary>
        private void ConvertToSwf(string pdfFilePath,string saveSwfFilePath,string fileName)
        {
            try
            {
                int flag = 0;
                int pageCount = GetPageCount(pdfFilePath);//计算pdf的页数
                string swfUrl = string.Empty;

                if (Directory.Exists(saveSwfFilePath) == false)
                    Directory.CreateDirectory(saveSwfFilePath);

                string exe = ConfigurationSettings.AppSettings["FlexPaper"];

                if (!File.Exists(exe))
                    throw new ApplicationException("Can not find: " + exe);

                StringBuilder sb = new StringBuilder();

                if (pageCount % 5 > 0)//每5页转换成为一个swf文件 这个细节很重要,我是每五页转换成一个swf文件,这样可以方便实现预装载。
                    flag = 1;
                else
                    flag = 0;

                for (var i = 0; i < (pageCount / 5 + flag); i++)
                {
                    swfUrl = saveSwfFilePath + "\\" + fileName + "-" + (i * 5 + 1).ToString() + "-" + ((i + 1) * 5) + ".swf";
                    sb.Append(exe);
                    sb.Append(" -o \"" + swfUrl + "\"");//output 
                    sb.Append(" -z");
                    sb.Append(" -s flashversion=9");//flash version 
                    sb.Append(" -s disablelinks");//禁止PDF里面的链接 
                    sb.Append(" -p " + (i * 5 + 1) + "-" + ((i + 1) * 5));//page range 
                    sb.Append(" -j 100");//Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85) 
                    sb.Append(" \"" + pdfFilePath + "\"");//input 

                    string strOutput = null;
                    Process proc = new Process();
                    proc.StartInfo.FileName = "cmd";
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.RedirectStandardInput = true;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    proc.Start();

                    proc.StandardInput.WriteLine(sb.ToString());
                    proc.StandardInput.WriteLine("exit");
                    strOutput = proc.StandardOutput.ReadToEnd();

                    Console.WriteLine(strOutput);
                    proc.WaitForExit();
                    proc.Close();
                    sb.Remove(0, sb.Length);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Info("转化成为swf文件错误" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 计算pdf文件总页数
        /// </summary>
        /// <param name="pdfPath"></param>
        /// <returns></returns>
        public static int GetPageCount(string pdfPath)
        {
            try
            {
                byte[] buffer = File.ReadAllBytes(pdfPath);
                int length = buffer.Length;
                if (buffer == null)
                    return -1;

                if (buffer.Length <= 0)
                    return -1;

                string pdfText = Encoding.Default.GetString(buffer);
                System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
                System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);

                return matches.Count;
            }
            catch (Exception ex)
            {
                LogHelper.Info("计算pdf文件总页数错误" + ex.Message);
                throw ex;
            }
        }

        补充,其实上述代码网上很多,我只是在里面增加了一些我的业务要求。

        好了,文档的转换大功告成,我们可以通过flexpaper播放器来加载所转换成的swf文件了,至于怎么实现预装载,还需要对flexpaper进行一些小的二次开发。当然,目前已经可以实现文档的在线播放了。

        罢了,罢了,我在设计在线文库中的一些小记(在线文库系列二之文档转换)就写到这里,我后续还会写如何把转换的实际操作如何与业务分离。这样可以提升用户体验的同时实现文档转换的自动化。

        敬请期待,我是百灵。祝大家天天好心情。

抱歉!评论已关闭.