ASP.NET 多媒體電子報刊設計(一)

來源:互聯網
上載者:User

電子報刊一般是採用PDF的方式進行上傳,在上傳之後,需要將PDF檔案進行轉換,在尋找了無數方案之後,還是確定下來使用ghostscript,畢竟它免費又開源。ghostscript可以在http://www.ghostscript.com/下載。這裡貼使用進程方式來轉換PDF,將PDF每一頁都轉換成圖片。

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Collections.Generic;using System.IO;using System.Text.RegularExpressions;using System.Diagnostics;namespace FlexPaperExample.WebDemo{    /// <summary>    /// PDF協助類    /// </summary>    public class PDFHelper    {        /// <summary>        /// 轉換圖片        /// </summary>        /// <param name="InputFile">源檔案完整路徑</param>        /// <param name="deletePDF">是否刪除</param>        /// <param name="filename">目標完整路徑</param>        /// <returns></returns>        public static IList<string> GenerateThumbnailImage(string InputFile, bool deletePDF, string filename)        {            return GenerateImage(InputFile, deletePDF, filename, "-dSAFER -dBATCH -dNOPAUSE -r50 -sDEVICE=jpeg -dGraphicsAlphaBits=4");        }        /// <summary>        /// 轉換圖片        /// </summary>        /// <param name="InputFile">源檔案完整路徑</param>        /// <param name="deletePDF">是否刪除PDF</param>        /// <param name="filename">目標完整路徑</param>        /// <param name="Arguments">參數設定</param>        /// <returns></returns>        private static IList<string> GenerateImage(string InputFile, bool deletePDF, string filename, string Arguments)        {            IList<string> result = new List<string>();            int PDFPageCount = GetPDFPageCount(InputFile);            if (PDFPageCount == 0)            {                return result;            }            string OutputFile = filename;            string extOut = Path.GetExtension(OutputFile);            string partOut = OutputFile.Remove(OutputFile.Length - extOut.Length, extOut.Length);            if (PDFPageCount == 1)            {                OutputFile = partOut + ".jpg";                result.Add(OutputFile);                if (File.Exists(OutputFile))                {                    File.Delete(OutputFile);                }            }            else            {                for (int i = 0; i < PDFPageCount; i++)                {                    string eachFileName = partOut + "_" + (i + 1).ToString() + ".jpg";                    result.Add(eachFileName);                    if (File.Exists(eachFileName))                    {                        File.Delete(eachFileName);                    }                }                OutputFile = OutputFile.Remove(OutputFile.Length - extOut.Length, extOut.Length);                OutputFile += "_%d.jpg";            }            ProcessStartInfo info = new ProcessStartInfo();            info.CreateNoWindow = true;            info.WindowStyle = ProcessWindowStyle.Hidden;            info.WorkingDirectory = System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"];            info.Arguments = Arguments + @" -sOutputFile=" + OutputFile + "  " + InputFile;            info.FileName = @"gswin32c.exe";            Process subProcess = new Process();            subProcess.StartInfo = info;            subProcess.Start();            subProcess.WaitForExit(int.MaxValue);            if (deletePDF)            {                System.IO.File.Delete(InputFile);            }            return result;        }        /// <summary>        /// 擷取pdf檔案的頁數        /// </summary>        /// <param name="path">檔案路徑</param>        /// <returns></returns>        private static int GetPDFPageCount(string strPath)        {            FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);            StreamReader r = new StreamReader(fs);            string pdfText = r.ReadToEnd();            Regex rx1 = new Regex(@"/Type\s*/Page[^s]");            MatchCollection matches = rx1.Matches(pdfText);            r.Close();            r.Dispose();            fs.Close();            fs.Dispose();            return matches.Count;        }    }}

使用方法非常簡單,調用GenerateThumbnailImage方法,其會返回一個IList泛型集合,裡面裝的是轉換之後的圖片完整路徑集合。

ghostscript的協助文檔可以在安裝之後,在Doc中找到Use.htm進行查看。

幾個比較關鍵的參數,-r50,圖片的象素,-sDEVICE=jpeg,檔案的類型。

需要檔案是gs安裝之後bin目錄下的四個檔案,分別是gsdll32.dll、gsdll32.lib、gswin32.exe、gswin32c.exe。

若有疑問或不正之處,歡迎提出指正和討論。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.