時間:2023-05-30 11:51:01 | 來源:網(wǎng)站運營
時間:2023-05-30 11:51:01 來源:網(wǎng)站運營
文庫網(wǎng)站開發(fā)轉換搭建——文檔轉換:文檔何時轉換,是人為轉換呢?還是由計算機自動轉換? /// <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時出發(fā)生錯誤" + ex.Message); throw ex; } }
三、完成了向pdf的轉換過程、接下來我們實現(xiàn)向swf的轉換。/// <summary> /// 將用戶所上傳文件轉化成為swf文件 ///pdfFilePath,就是要轉換的pdf文件路徑 /// </summary> private void ConvertToSwf(string pdfFilePath,string saveSwfFilePath,string fileName) { try { int flag = 0; int pageCount = GetPageCount(pdfFilePath);//計算pdf的頁數(shù) 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文件 這個細節(jié)很重要,我是每五頁轉換成一個swf文件,這樣可以方便實現(xiàn)預裝載。 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文件總頁數(shù) /// </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文件總頁數(shù)錯誤" + ex.Message); throw ex; } }
好了,文檔的轉換大功告成,這是我在網(wǎng)上看到了一些關于文庫網(wǎng)站轉換學習的,大家都可以參考借鑒。文庫網(wǎng)站開發(fā)有什么疑問可以私信(q:3328752804),大家一起交流進步。關鍵詞:轉換,文庫,建文
微信公眾號
版權所有? 億企邦 1997-2025 保留一切法律許可權利。