C# PDF轉Image圖片
C# PDF轉Image圖片
概述
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格式的圖片;
收藏的文章 今天又使用了一下 插件安裝路徑不要有中文 不然功能失效 找了半天 真是坑人


浙公網安備 33010602011771號