ASP.NET 多媒体电子报刊设计(一)
电子报刊一般是采用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。
若有疑问或不正之处,欢迎提出指正和讨论。