C# PDF to image

本文介绍如何利用Ghostscript这一免费工具生成PDF文件的缩略图。通过C#代码示例展示了批量处理PDF页面并转换为JPEG格式图片的方法。

可以使用Ghostscript ,免费的,我们一直在用。下载地址 http://sourceforge.net/project/showfiles.php?group_id=1897&package_id=108733

C# code
public IList<string> GenerateThumbnailImage(string InputFile, bool deletePDF, string filename) { return GenerateImage(InputFile, deletePDF, filename, "-dSAFER -dBATCH -dNOPAUSE -r150 -sDEVICE=jpeg -dGraphicsAlphaBits=4"); } private IList<string> GenerateImage(string InputFile, bool deletePDF, string filename,string Arguments) { IList<string> result = new List<string>(); PDFLibrary myPDFLibrary = new PDFLibrary(); int PDFPageCount = myPDFLibrary.GetPageCount(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; }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值