文库网站开发转换搭建——文档转换

222 阅读2分钟

文档何时转换,是人为转换呢?还是由计算机自动转换?

一、不同转换:

(1)、人工转换:

用户上传文件 —> 文档编辑专员对用户所上传文档进行审核。并设置审核标识 —> 管理员择时对已经通过审核的文档进行转换

(2)、计算机转换:

用户上传文件 —> 计算机初审 —> 计算机启动文档转换程序 对文档转换 —> 同时启动进程监控服务,对死锁转转换程序进行关闭,释放内存资源

补充:在整个文档业务中,我们希望加入举报业务,即对不良文档进行举报。(管理员可以针对被举报文档做出有针对性的管理)

二、由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;
            }
        }

好了,文档的转换大功告成,这是我在网上看到了一些关于文库网站转换学习的,大家都可以参考借鉴。文库网站开发有什么疑问可以私信或微信kjwenlc联系我,大家一起交流进步。