时间:2023-05-30 11:51:01 | 来源:网站运营
时间:2023-05-30 11:51:01 来源:网站运营
文库网站开发转换搭建——文档转换:文档何时转换,是人为转换呢?还是由计算机自动转换? /// <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的转换。/// <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; } }
好了,文档的转换大功告成,这是我在网上看到了一些关于文库网站转换学习的,大家都可以参考借鉴。文库网站开发有什么疑问可以私信(q:3328752804),大家一起交流进步。关键词:转换,文库,建文