http://blog.csdn.net/suoxd123/archive/2010/01/08/5157668.aspx
下面提供一个完整的事例代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; using Acrobat; namespace PDFLibSharp { public class PDFLibSharp { private string filename; int page; /// <summary> /// the construct function to create the filename and page /// </summary> /// <param name="filenamesource"></param> /// <param name="pagesource"></param> public int ConvertPtoI(string filenamesource, int pagesource) { filename = filenamesource; page = pagesource; //assert the file is pdf or not if(!(filename.EndsWith(".pdf") || filename.EndsWith(".PDF"))) { return 0; //0 represent there is no file } //begin to change try { Acrobat.CAcroPDDoc pdfDoc; pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", ""); bool ret = pdfDoc.Open(filename); if(!ret) { return 1; //1 can not open to read } //the page is too big int pageCount = pdfDoc.GetNumPages(); if(page >= pageCount || page <= 0) { return 2; } //get the page and the size Acrobat.CAcroPDPage pdfPage; pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(page); Acrobat.CAcroPoint pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize(); Acrobat.CAcroRect pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", ""); pdfRect.Left = 0; pdfRect.right = pdfPoint.x; pdfRect.Top = 0; pdfRect.bottom = pdfPoint.y; pdfPage.CopyToClipboard(pdfRect, 0, 0, 100); IDataObject clipboardData = Clipboard.GetDataObject(); if (clipboardData.GetDataPresent(DataFormats.Bitmap)) { Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap); int thumbnailWidth = 1000; int thumbnailHeight = 1000; Image pdfImage = pdfBitmap.GetThumbnailImage(thumbnailWidth, thumbnailHeight,null, IntPtr.Zero); Bitmap thumbnailBitmap = new Bitmap(thumbnailWidth + 7, thumbnailHeight + 7,System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics thumbnailGraphics = Graphics.FromImage(thumbnailBitmap)) { thumbnailGraphics.DrawImage(pdfImage, 2, 2, thumbnailWidth, thumbnailHeight); string outputFile = filename.Substring(0, filename.Length - 4) + '_' + page.ToString() + ".png"; thumbnailBitmap.Save(outputFile, System.Drawing.Imaging.ImageFormat.Png); Console.WriteLine("Generated thumbnail... {0}", outputFile); } pdfDoc.Close(); Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); } } catch (System.Exception ex) { return 10; } return 5; } } }