<p><pre name="code" class="csharp"><p>公司需要将PDF转为图片,在网上查找了下发现有篇关于转换的帖子很不错。具体地址:http://blog.csdn.net/shi0090/article/details/7262199</p><p>为了稳定我选择了使用<span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">【Acrobat.dll】,通过Adobe官方提供的接口,实现PDF转图片,优点是官方提供,稳定性高。</span></p><p><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">但却面临几个问题,</span></p><p><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">第一:需要安装安装Adobe Acrobat X Pro,且不支持多线程。</span></p><p><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">第二:web服务是不能获取剪贴板上的内容(在winfrom下可以通过剪贴板获取到转换后的图片)</span></p><p>因为我需要在web服务中去使用该方法,所以只能放弃,选择了之前博主说的<span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">较稳定的 </span><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">O2S.Components.PDFRender4NET.dll</span></p><p><span style="font-family: 'Microsoft YaHei';"><span style="font-size: 14px; line-height: 26px;">因为需要打水印,所以我从博主的资源里下了个破解版无水印的dll,然后在拿到图片后进行了加水印操作。</span></span></p><p><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">废话不多说 附代码</span></p><p><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;"><span style="white-space:pre"> </span> using O2S.Components.PDFRender4NET; <span style="white-space:pre"> </span> using System; <span style="white-space:pre"> </span> using System.Collections.Generic; <span style="white-space:pre"> </span> using System.Configuration; <span style="white-space:pre"> </span> using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web; using System.Web.Services;</span></p> /// <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> /// <param name="mark">设置水印</param> public static void PDF2Images(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, float resolution, string mark) { //打开PDF PDFFile pdfFile = PDFFile.Open(pdfInputPath); //判断输出路径是否存在,如果不存在则创建 if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } //判断开始页数 如果为0 则默认从第一页开始 if (startPageNum <= 0) { startPageNum = 1; } //判断PDF总页数 if (endPageNum < pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } //如果参数页参数传反,自动纠正 if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } //默认图片格式 if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; } //根据总页数进行遍历 for (int i = startPageNum; i < endPageNum; i++) { //获取图片对象 Bitmap pageImage = pdfFile.GetPageImage(i, 56 * resolution); //设置水印 Graphics g = Graphics.FromImage(pageImage); Font font = new Font("Verdana", 16f, FontStyle.Bold, GraphicsUnit.Point); g.DrawString(mark, font, Brushes.Red, 2, 1); font.Dispose(); //将图片按照指定格式保存到本地 pageImage.Save(imageOutputPath + i.ToString() + "." + imageFormat.ToString(), imageFormat); pageImage.Dispose(); } pdfFile.Dispose(); }
</pre><pre name="code" class="csharp"><span style="white-space:pre"> </span>同时附上winform里面使用<span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">Acrobat.dll的源码</span>
<span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;"><span style="white-space:pre"> </span> using System; <span style="white-space:pre"> </span> using System.Collections.Generic; <span style="white-space:pre"> </span> using System.Drawing; <span style="white-space:pre"> </span> using System.Drawing.Imaging; <span style="white-space:pre"> </span> using System.IO; <span style="white-space:pre"> </span> using System.Linq; <span style="white-space:pre"> </span> using System.Runtime.InteropServices; <span style="white-space:pre"> </span> using System.Threading; <span style="white-space:pre"> </span> using System.Timers; <span style="white-space:pre"> </span> using System.Windows.Forms; <span style="white-space:pre"> </span> using PDFConvertService.Comm; <span style="white-space:pre"> </span> using PDFConvertService.DAL; <span style="white-space:pre"> </span> using PDFConvertService.Entity;</span>
/// <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 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, "") + i.ToString() + imageName + "." + imageFormat.ToString(), imageFormat); pdfBitmap.Dispose(); } } pdfDoc.Close(); Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); Marshal.ReleaseComObject(pdfPoint); }
赶Demo中,做出来会放上。
时间: 2024-09-30 20:47:21