标签:server ima tor 第三方 void c# oid gic ida
概述
PDF是常用的文件格式之一,通常情况下,我们可以使用itextsharp生产PDF文件;可是如何将PDF文件转换成图片那?目前常用的:思路1、根据PDF绘画轨迹重新绘制图片;思路2、是将PDF文件解析成二进制,直接将二级制转换成图片;借助这2种思路,我在网上和同事的帮助下找到了2个DLL文件(第三方);
思路1:
使用第三方DLL:O2S.Components.PDFRender4NET DLL下载
编写代码部分:
public enum Definition { One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10 } public class PDFTranImgHelp { /// <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(string 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(); } }
调用部分:
PDFTranImgHelp.ConvertPDF2Image("F:\\204834.pdf", "F:\\", "NImage", 1, 1, ImageFormat.Png, Definition.Five);
不足:
如果预解析的原PDF文件中,含有png透明的图片,使用该方式解析失败!
思路二:
使用的第三方类库是:Magick.NET-Q16-AnyCPU.dll DLL下载(包括Lib下的文件)
编写部分代码:
public class PDFTranImg { public static byte[] ConvertPDF2Image(byte[] PDFbytes,string ImgPath) { try { //设置dll文件的目录 string DLLLibPath = AppDomain.CurrentDomain.BaseDirectory; string dlllib = DLLLibPath + "lib"; MagickNET.SetGhostscriptDirectory(dlllib); MagickReadSettings setting = new MagickReadSettings(); // Settings the density to 300 dpi will create an image with a better quality setting.Density = new Density(100); using (MagickImageCollection images = new MagickImageCollection()) { // 读取二进制数组中的文件 images.Read(PDFbytes, setting); using (MagickImage vertical = images.AppendVertically()) { vertical.Write(ImgPath); byte[] ReusltByte = File.ReadAllBytes(ImgPath); return ReusltByte; } } } catch (Exception ex) { return null; } finally { File.Delete(ImgPath); } } }
调用部分代码:
public FileContentResult EPDFCodePic(string InvoiceCodeNumber) { string[] InvoiceCodeNumber1 = PDFUnEncode(InvoiceCodeNumber); string ResultPDF64 = LoadPDFImportTemplate(InvoiceCodeNumber1[0], InvoiceCodeNumber1[1], InvoiceCodeNumber1[2], InvoiceCodeNumber1[3]); byte[] PDFBytes = Convert.FromBase64String(ResultPDF64); string PDFTempFilePath = System.Web.HttpContext.Current.Server.MapPath("Temp"); string sPath = PDFTempFilePath + "\\" + DateTime.Now.ToString("yyyyMM"); if (!Directory.Exists(sPath)) { Directory.CreateDirectory(sPath); } string SaveAsFileImagePath = sPath + "\\" + InvoiceCodeNumber1[0]+InvoiceCodeNumber1[1] + ".gif"; byte[] ResutlBytes= PDFTranImg.ConvertPDF2Image(PDFBytes,SaveAsFileImagePath); return File(ResutlBytes, @"image/gif"); }
不足:
使用该方式只能将PDF解析成gif格式的图片;
标签:server ima tor 第三方 void c# oid gic ida
原文地址:http://www.cnblogs.com/xibei666/p/7012807.html