標籤: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格式的圖片;
C# PDF轉Image圖片