• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-02-28) 16次浏览 已收录 0个评论

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

一、不同转换:

(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分割我,大家一起交换提高。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:文库网站开发转换搭建文档转换

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址