/// <summary> /// Converting PDF Files TO Specified Image Format /// </summary> /// <param name="sourceFileName">Source PDF File Path</param> /// <param name="DestinationPath">Destination PDF File Path</param> /// <param name="outPutImageFormat">Type Of Exported Image</param> /// <returns>Returns Count Of Exported Images</returns> public int Convert(string sourceFileName, string DestinationPath, ImageFormat outPutImageFormat) { if (pdfDoc.Open(sourceFileName)) { // pdfapp.Hide(); pageCount = pdfDoc.GetNumPages(); for (int i = 0; i < pageCount; i++) { pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i); pdfPoint = (Acrobat.AcroPoint)pdfPage.GetSize(); pdfRect.Left = 0; pdfRect.right = pdfPoint.x; pdfRect.Top = 0; pdfRect.bottom = pdfPoint.y; pdfPage.CopyToClipboard(pdfRect, 0, 0, 100); string outimg = ""; string filename = sourceFileName.Substring(sourceFileName.LastIndexOf("\\")); if (pageCount == 1) { outimg = DestinationPath + "\\" + filename + "." + outPutImageFormat.ToString(); } else { outimg = DestinationPath + "\\" + filename + "_" + i.ToString() + "." + outPutImageFormat.ToString(); } Clipboard.GetImage().Save(outimg, outPutImageFormat); ////////////Firing Progress Event OnExportProgressChanging(outimg); } Dispose(); } else { Dispose(); throw new System.IO.FileNotFoundException(sourceFileName + " Not Found!"); } return(pageCount); }
/// <summary> /// 将PDF文档转换为图片的方法,你可以像这样调用该方法:ConvertPDF2ImageO2S("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) { // Is NULL if (pdfInputPath == null) { m_nConvertStatus = EErrorType.PTJ_FILENAME_EMPTY; } if (!System.IO.File.Exists(pdfInputPath)) { m_nConvertStatus = EErrorType.WPS_FILE_NOTEXISTS; } Acrobat.CAcroPDDoc pdfDoc = null; Acrobat.CAcroPDPage pdfPage = null; Acrobat.CAcroRect pdfRect = null; Acrobat.CAcroPoint pdfPoint = null; ewhCopyJpg = new EventWaitHandle(false, 0, "WPSToJPGPDFToJPGUsingArobat"); try { // 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(m_strOutDir)) { Directory.CreateDirectory(m_strOutDir); } m_nTotalPage = pdfDoc.GetNumPages(); PrintLog("ConvertStatus:" + (int)ConvertStatus.START + " " + 0 + " " + m_nTotalPage); // start to convert each page for (int i = 1; i <= m_nTotalPage; i++) { PrintLog("ConvertStatus:" + (int)ConvertStatus.ING + " " + i + " " + m_nTotalPage); pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1); pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize(); pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", ""); // 如果当前分辨率少于1280*720,强制放大 int imgWidth = (int)((double)pdfPoint.x * (int)m_dQuality); int imgHeight = (int)((double)pdfPoint.y * (int)m_dQuality); pdfRect.Left = 0; pdfRect.right = (short)imgWidth; pdfRect.Top = 0; pdfRect.bottom = (short)imgHeight; // 临界区 ewhCopyJpg.WaitOne(); // 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 * (int)m_dQuality)); IDataObject clipboardData = Clipboard.GetDataObject(); if (clipboardData.GetDataPresent(DataFormats.Bitmap)) { string strJPGName = m_strOutDir + "\\" + Path.GetFileNameWithoutExtension(pdfInputPath) + "_" + pdfDoc.GetNumPages() + "_" + (i).ToString() + ".jpg"; Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap); pdfBitmap.Save(strJPGName, ImageFormat.Jpeg); pdfBitmap.Dispose(); } Clipboard.Clear(); ewhCopyJpg.Set(); } QuitAcrobat(ref pdfDoc, ref pdfPage, ref pdfRect, ref pdfPoint); } catch (Exception ex) { PrintLog(ex.Message.ToString()); m_nConvertStatus = EErrorType.PTJ_EXCEPTION_FAILED; } finally { QuitAcrobat(ref pdfDoc, ref pdfPage, ref pdfRect, ref pdfPoint); } }
/// <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) + i.ToString() + "." + imageFormat.ToString(), imageFormat); pdfBitmap.Dispose(); } } pdfDoc.Close(); Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); Marshal.ReleaseComObject(pdfPoint); }
private void GetImgFromClipboard() { try { string pdfInputPath = tempFilePath; string imageOutputPath = savePath; //string pdfInputPath = @"D:\pdfImgs\test.pdf"; //string imageOutputPath = @"D:\pdfImgs\"; int startPageNum = 1; ImageFormat imageFormat = ImageFormat.Jpeg; int resolution = 1; 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); } int endPageNum = pdfDoc.GetNumPages(); 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 bool b = pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution)); IDataObject clipboardData = Clipboard.GetDataObject(); if (clipboardData.GetDataPresent(DataFormats.Bitmap)) { Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap); string imtName = i.ToString() + ".jpg"; pdfBitmap.Save(imageOutputPath + "\\" + imtName, imageFormat); pdfBitmap.Dispose(); } } if (endPageNum > 0) { StringBuilder sb = new StringBuilder(); sb.Append(string.Format("delete from PreviewFile where AttachmentId={0};", lastCheckId)); for (int num = 1; num <= endPageNum; num++) { string filename = imageOutputPath + "\\" + num.ToString() + ".jpg"; sb.Append(string.Format("insert into PreviewFile(AttachmentId,SavePath,CreateTime)values({0},'{1}','{2}');", lastCheckId, filename.Replace(sourcePath, "").Replace("\\", "/"), DateTime.Now)); Thread.Sleep(1000); } var result = SqlHelper.ExecteNonQueryText(sb.ToString(), null); if (result > 0) { } else { RecodeErroeData(lastCheckId); } } pdfDoc.Close(); Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); Marshal.ReleaseComObject(pdfPoint); } catch (Exception ex) { LogControl.LogInfo("从剪切板获取PDF图片异常lastCheckId:" + lastCheckId + " " + ex.ToString()); RecodeErroeData(lastCheckId); } }
/// <summary> /// PDF转Image /// </summary> /// <param name="pdfInputPath">PDF文件</param> /// <param name="imageOutputPath">图片文件路径</param> /// <param name="imageName">图片名称</param> /// <param name="startPageNum">从第几页开始</param> /// <param name="endPageNum">到第几页结束</param> /// <param name="imageFormat">生成图片类型</param> /// <param name="resolution">显示倍数</param> public bool ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, string Hospsampleid, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution) { bool falg = true; List <string> Sqllst = new List <string>(); //执行的SQL集合 string errormsg = ""; SortedList SQLlist = new SortedList(new MySort()); int errcount = 0; //标记当前pdf每页是否都已经转图片成功 Acrobat.CAcroPDDoc pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", ""); Acrobat.CAcroPDPage pdfPage = null; Acrobat.CAcroRect pdfRect = null; Acrobat.CAcroPoint pdfPoint = null; try { //yhl 2017.3.22pdf转图片之前先清空粘贴板信息,防止图片信息没清空,下个条码存了上个条码的信息 //IDataObject clipboardData1 = Clipboard.GetDataObject(); //if (clipboardData1.GetDataPresent(DataFormats.Bitmap)) //{ Clipboard.Clear(); // } // 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", "");//.CreateObject("AcroExch.PDDoc", ""); //var CreatePdf = Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", ""); // pdfDoc = (Acrobat.CAcroPDDoc)CreatePdf; // pdfPage.CopyToClipboard(pdfRect, 0, 0, 100); // 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; 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) + "_" + i + ".jpg", imageFormat); pdfBitmap.Dispose(); } string imgpath = Path.Combine(imageOutputPath, imageName) + "_" + i + ".jpg"; if (!File.Exists(imgpath) || File.ReadAllBytes(imgpath).Count() <= 0) { errcount++; } string imageAddress = getImageString(Path.Combine(imageOutputPath, imageName) + "_" + i + ".jpg"); int maxid = new TableIDDAO().GetNextId("DAAN_LIS_REPORT"); DAAN_LIS_REPORT dalisReport = new DAAN_LIS_REPORT(); dalisReport.DAAN_ID = maxid.ToString(); dalisReport.DAAN_JPG = Convert.FromBase64String(imageAddress); dalisReport.DAAN_BAR_CODE = Hospsampleid; SQLlist.Add(new Hashtable() { { "INSERT", "InsertDAAN_LIS_REPORT" } }, dalisReport); } pdfDoc.Close(); Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); Marshal.ReleaseComObject(pdfPoint); falg = service.ExecuteSqlTran(SQLlist, ref errormsg); } catch (Exception ex) { string message = ex.Message; SetTB(imageName + "条码转换失败!" + ex.Message); falg = false; errcount++; pdfDoc.Close(); Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); Marshal.ReleaseComObject(pdfPoint); } if (errcount > 0) { falg = false; } return(falg); }
private Boolean pdf_exec(OfficeDomain office) { Boolean result = false; log("pdf开始工作..."); if (File.Exists(office.pdfFileName)) { Acrobat.CAcroPDPage pdfPage; Acrobat.CAcroPoint pdfPoint; int pageCount = 0; double resolution = 1.2; //打开 pdf bool ret = pdfDoc.Open(office.pdfFileName); if (!ret) { error("pdf打开失败了,尝试重启pdf程序"); pdf_start(); } else { try { //pdf 页码 pageCount = pdfDoc.GetNumPages(); log("pdf检测页码:{0}", pageCount); //缓存文件 office.result = "+" + pageCount; string info = Path.Combine(office.fileName, "0.info"); if (!File.Exists(info)) { File.WriteAllText(info, pageCount + ""); log("成功缓存页码"); } //循环生成pdf for (int i = 0; i < pageCount; i++) { string pngName = Path.Combine(office.fileName, i + "") + ".png"; if (File.Exists(pngName)) { warn("第 {0} 张 skip", i + 1); continue; } pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i); pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize(); int imgWidth = (int)((double)pdfPoint.x * resolution); int imgHeight = (int)((double)pdfPoint.y * resolution); pdfRect.right = (short)imgWidth; pdfRect.Left = 0; pdfRect.Top = 0; pdfRect.bottom = (short)imgHeight; pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution)); IDataObject clipboardData = Clipboard.GetDataObject(); if (clipboardData != null && clipboardData.GetDataPresent(DataFormats.Bitmap)) { Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap); if (pdfBitmap != null) { pdfBitmap.Save(pngName, ImageFormat.Png); pdfBitmap.Dispose(); log("第 {0} 张 created", i); } else { error("第 {0} 张 error", i); } } else { error("第 {0} 张 error", i); } Marshal.ReleaseComObject(pdfPage); } result = true; log("pdf工作完毕,准备关闭文件"); try { pdfDoc.Close(); log("pdf文件关闭成功"); } catch (Exception e) { error("pdf文件关闭失败:{0},尝试重启程序", e.Message); pdf_start(); } } catch (Exception e2) { error("pdf转换失败:{0}", e2.Message); } } } else { error("pdf工作时发现PDF文件不存在"); } return(result); }
/* private static void TurnTheImageToPdf(ref List<System.Drawing.Image> allName) * { * throw new NotImplementedException(); * }*/ //ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution) //将pdfInputPath改成得到的pdfToPdfPath路径 /*public static void ConvertPDF2Image(List<string> pdfInputPath,string outpath ,int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution) * { * string imageinputPath=pdfInputPath[0]; * string imageOutputPath = outpath; * string imageName; * int flag=0; * Acrobat.CAcroPDDoc pdfDoc = null; * Acrobat.CAcroPDPage pdfPage = null; * Acrobat.CAcroRect pdfRect = null; * Acrobat.CAcroPoint pdfPoint = null; * pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", ""); * if (!pdfDoc.Open(imageinputPath)) * { * throw new FileNotFoundException(); * } * * if (!Directory.Exists(imageOutputPath)) * { * Directory.CreateDirectory(imageOutputPath); * } * * if (startPageNum <= 0) * { * startPageNum = 1; * } * if (endPageNum > pdfDoc.GetNumPages()) * { * endPageNum = pdfDoc.GetNumPages(); * } * if (startPageNum > endPageNum) * { * int tempPageNum = startPageNum; * startPageNum = endPageNum; * endPageNum = startPageNum; * } * for(int i=0;i< pdfInputPath.Count; i++) * { * string fileName = pdfInputPath[i].Substring(0, pdfInputPath[i].Length - 4); * imageOutputPath = pdfInputPath[i].Substring(0, pdfInputPath[i].Length - 4);//这里有问题 * imageName = fileName; * flag++; * for (int j = startPageNum; j <= endPageNum; j++) * { * pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(j - 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; * 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)+ ".Jpeg", imageFormat); * pdfBitmap.Dispose(); * } * } * * * } * if (flag == pdfInputPath.Count) { * MessageBox.Show("转化成功"); * } * * }*/ //将小pdf转换成图片 public static string ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution) { string imagePath = null; Acrobat.CAcroPDDoc pdfDoc = null; Acrobat.CAcroPDPage pdfPage = null; Acrobat.CAcroRect pdfRect = null; Acrobat.CAcroPoint pdfPoint = null; pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", ""); if (!pdfDoc.Open(pdfInputPath)) { throw new FileNotFoundException(); } if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfDoc.GetNumPages()) { endPageNum = pdfDoc.GetNumPages(); } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } 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; 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) + ".Png", imageFormat); pdfBitmap.Dispose(); } } pdfDoc.Close(); imagePath = imageName + ".Png"; System.Runtime.InteropServices.Marshal.ReleaseComObject(pdfPage); System.Runtime.InteropServices.Marshal.ReleaseComObject(pdfRect); System.Runtime.InteropServices.Marshal.ReleaseComObject(pdfDoc); System.Runtime.InteropServices.Marshal.ReleaseComObject(pdfPoint); return(imagePath); }