PDF转图片(O2S.Components.PDFRender4NET )
1.PDF生成IMG的帮助类
using O2S.Components.PDFRender4NET; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MysteelData.Common { public static class PDFHelper { public enum Definition { One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10 } /// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public static void ConvertPDF2Image(Stream pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); pageImage.Dispose(); } pdfFile.Dispose(); } } }
2.MVC中使用
public ActionResult Details(long Id, string Title) { byte[] fileByte = null; try {
//压缩文件流 fileByte = CompressionUtilHelper.Decompress(MacroSpeBLL.GetMaHotAnalysisAttachPathById(Id)); Stream stream = new MemoryStream(fileByte); PDFFile pdfFile = PDFFile.Open(stream); int number = pdfFile.PageCount; string imglist = string.Empty; string imageName = Id + "_mysteel" + "_"; for (int j = 1; j <= number; j++) { string imgExtensionName = imageName + j + ".Jpeg"; if (!new FileInfo(Server.MapPath("~/PdfImages/" + imgExtensionName)).Exists) { PDFHelper.ConvertPDF2Image(stream, files, imageName, j, j, ImageFormat.Jpeg, PDFHelper.Definition.Three); } imglist += imgExtensionName + ","; } return Content(imglist.Substring(0, imglist.Length - 1)); } catch (Exception) { return Content(string.Empty); } }
3.涉及到的文件流压缩类
using System.IO; using System.IO.Compression; using System.Linq; namespace MySteel.Common.Helper { /// <summary> /// 压缩处理数据帮助类 /// </summary> public class CompressionUtilHelper { /// <summary> /// 压缩数据 /// </summary> /// <param name="source"></param> /// <param name="dest"></param> public static void Compress(Stream source, Stream dest) { using (GZipStream zipStream = new GZipStream(dest, CompressionMode.Compress, true)) { byte[] buf = new byte[1024]; int len; while ((len = source.Read(buf, 0, buf.Length)) > 0) { zipStream.Write(buf, 0, len); } } } /// <summary> /// 解压数据 /// </summary> /// <param name="source"></param> /// <param name="dest"></param> public static void Decompress(Stream source, Stream dest) { using (GZipStream zipStream = new GZipStream(source, CompressionMode.Decompress, true)) { byte[] buf = new byte[1024]; int len; while ((len = zipStream.Read(buf, 0, buf.Length)) > 0) { dest.Write(buf, 0, len); } } } /// <summary> /// 压缩数据 /// </summary> /// <param name="Source"></param> public static byte[] Compress(byte[] Source) { MemoryStream stream = new MemoryStream(); GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress); gZipStream.Write(Source, 0, Source.Length); gZipStream.Close(); return stream.ToArray(); } ///// <summary> ///// 解压数据 ///// </summary> ///// <param name="Source"></param> public static byte[] Decompress(byte[] Source) { MemoryStream stream = new MemoryStream(); GZipStream gZipStream = new GZipStream(new MemoryStream(Source), CompressionMode.Decompress); byte[] b = new byte[4096]; int count = 0; while (true) { int n = gZipStream.Read(b, 0, b.Length); if (n > 0) { stream.Write(b, 0, n); count += n; } else { gZipStream.Close(); break; } } return stream.ToArray().Take(count).ToArray(); } } }