private void ExportAsImage(string strFile, string strExportPath) { if (!Directory.Exists(strExportPath)) { Directory.CreateDirectory(strExportPath); } Acrobat.AcroPDDoc pdf = new AcroPDDoc(); pdf.Open(strFile); int pageCount = Convert.ToInt32(txtPage.Text); int count = pdf.GetNumPages(); for (int i = 0; i < count; i++) { if (i == pageCount) { break; } listMsg.Items.Add($"正在导出第{i + 1}页(共{count}页)"); listMsg.TopIndex = listMsg.Items.Count - 1; int zoom = 1; while (true) { try { CAcroPDPage page = (CAcroPDPage)pdf.AcquirePage(i); CAcroRect pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", ""); CAcroPoint pdfPoint = (Acrobat.CAcroPoint)page.GetSize(); int imgWidth = (int)((double)pdfPoint.x * zoom); int imgHeight = (int)((double)pdfPoint.y * zoom); //设置裁剪矩形的大小为当前页的大小 pdfRect.Left = 0; pdfRect.right = (short)imgWidth; pdfRect.Top = 0; pdfRect.bottom = (short)imgHeight; page.CopyToClipboard(pdfRect, 0, 0, (short)(100 * zoom)); Object obj = Clipboard.GetData(DataFormats.Bitmap); using (Bitmap bitmap = (Bitmap)obj) { SavePicture(bitmap, strExportPath, Path.GetFileNameWithoutExtension(strFile), (i + 1).ToString()); } } catch (Exception e) { continue; } break; } } }
private void ExportAsImages(string fileName, string filePath) { try { CAcroPDDoc acroPDDoc = new AcroPDDocClass(); if (!acroPDDoc.Open(filePath)) { Marshal.ReleaseComObject(acroPDDoc); acroPDDoc = null; throw new ApplicationException("Can't open file \"" + fileName + "\""); } int pageCount = acroPDDoc.GetNumPages(); if (pageCount == 2) { CAcroPDPage acroPDPage = (CAcroPDPage)acroPDDoc.AcquirePage(1); CAcroPoint acroPoint = (CAcroPoint)acroPDPage.GetSize(); CAcroRect acroRect = new AcroRectClass(); acroRect.Top = 0; acroRect.bottom = acroPoint.y; acroRect.Left = 0; acroRect.right = acroPoint.x; if (!acroPDPage.CopyToClipboard(acroRect, 0, 0, 100)) { Marshal.ReleaseComObject(acroPDPage); acroPDPage = null; Marshal.ReleaseComObject(acroPDDoc); acroPDDoc = null; throw new ApplicationException("Unknown error while extracting pages of " + filePath); } acroRect = null; acroPoint = null; Marshal.ReleaseComObject(acroPDPage); acroPDPage = null; IDataObject dataObject = Clipboard.GetDataObject(); Bitmap bitmap = (Bitmap)dataObject.GetData(DataFormats.Bitmap); string newFileName = fileName.Substring(0, fileName.LastIndexOf(".")) + "_Page2.jpg"; string targetPath = rootPath + "\\" + newFileName; if (File.Exists(targetPath)) { File.Delete(targetPath); } bitmap.Save(targetPath, ImageFormat.Jpeg); bitmap.Dispose(); } Marshal.ReleaseComObject(acroPDDoc); acroPDDoc = null; } catch { throw; } }
//http://www.myexception.cn/c-sharp/1506415.html //http://www.aspose.com/docs/display/pdfnet/Generate+Thumbnail+Images+from+PDF+Documents public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution) { CAcroPDDoc pdfDoc = null; CAcroPDPage pdfPage = null; CAcroRect pdfRect = null; 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 = (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 = (CAcroPDPage)pdfDoc.AcquirePage(i - 1); pdfPoint = (CAcroPoint)pdfPage.GetSize(); pdfRect = (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 != null && clipboardData.GetDataPresent(DataFormats.Bitmap)) { string _dir = Path.Combine(imageOutputPath, imageName); string _path = _dir + "_" + i.ToString() + ".jpg"; if (File.Exists(_path)) { File.Delete(_path); } Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap); pdfBitmap.Save(_path, imageFormat); pdfBitmap.Dispose(); } } pdfDoc.Close(); Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); Marshal.ReleaseComObject(pdfPoint); }