C#使用Adobe Acrobat将PDF转成图片

今日部门的部花有一小需求,需要将容量达40余G的PDF文件转换成Jpg的图片文件,而且PDF分散在各个目录中间。单个转换工作量大的无法想象!这么办呢?写程序解决吧。在网上找了很多资料无法解决。第三方转换程序不识别中文等等等等的问题。最终在找了很多资料发现Adobe官方Acrobat的一个接口可以将PDF转换成JPG。
程序首先解决的是将分散在各个目录中间的PDF文件找出来,目录下有子目录,子目录下又有子目录。因此有了上一篇文章C#递归方法遍历目录及子目录,这个问题解决后可以切入正题了。PDF转换JPG的函数:

[code lang=”csharp”]
/// <summary>
/// 将PDF文档转换为图片的方法,你可以像这样调用该方法:ConvertPDF2Image("F:\\A.pdf", "F:\\", "A", 0, 0, null, 0);
/// 因为大多数的参数都有默认值,startPageNum默认值为1,endPageNum默认值为总页数,
/// imageFormat默认值为ImageFormat.Jpeg,resolution默认值为1
/// </summary>
/// <param name="pdfInputPath">PDF文件路径</param>
/// <param name="imageOutputPath">图片输出路径</param>
/// <param name="imageName">图片的名字,不需要带扩展名</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换,默认值为1</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换,默认值为PDF总页数</param>
/// <param name="imageFormat">设置所需图片格式</param>
/// <param name="resolution">设置图片的分辨率,数字越大越清晰,默认值为1</param>
public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
{
Acrobat.CAcroPDDoc pdfDoc = null;
Acrobat.CAcroPDPage pdfPage = null;
Acrobat.CAcroRect pdfRect = null;
Acrobat.CAcroPoint pdfPoint = null;

// Create the document (Can only create the AcroExch.PDDoc object using late-binding)
// Note using VisualBasic helper functions, have to add reference to DLL
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

// validate parameter
if (!pdfDoc.Open(pdfInputPath)) { throw new FileNotFoundException(); }
if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; }
if (resolution <= 0) { resolution = 1; }

// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i – 1);
pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

int imgWidth = (int)((double)pdfPoint.x * resolution);
int imgHeight = (int)((double)pdfPoint.y * resolution);

pdfRect.Left = 0;
pdfRect.right = (short)imgWidth;
pdfRect.Top = 0;
pdfRect.bottom = (short)imgHeight;

// Render to clipboard, scaled by 100 percent (ie. original size)
// Even though we want a smaller image, better for us to scale in .NET
// than Acrobat as it would greek out small text
pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution));

IDataObject clipboardData = Clipboard.GetDataObject();

if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
pdfBitmap.Save(Path.Combine(imageOutputPath, imageName) + ".jpg", imageFormat);
pdfBitmap.Dispose();
}
}

pdfDoc.Close();
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
Marshal.ReleaseComObject(pdfPoint);
}[/code]

Acrobat的引用需要先安装Adobe Acrobat,下载地址不找了,大约500M。上面的代码调用方法都在注释中,最后把文件处理,界面处理写好之后就编译成功运行了。下面上运行截图。
20130609103702
最后再啰嗦一句,源代码奉上 点击下载PDF转JPG源码

本博客所有文章如无特别注明均为原创

如果觉得对你有帮助,可以通过下方打赏对作者表示鼓励

本文采用知识共享署名-非商业性使用-相同方式共享

如若转载,请注明出处:《C#使用Adobe Acrobat将PDF转成图片》https://www.fangsi.net/321.html

(19)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
锋哥的头像锋哥管理员
上一篇 2013年6月8日 16:05
下一篇 2013年6月16日

相关推荐

发表回复

登录后才能评论