Monday, April 9, 2012

Thumbnail View of PDF


Pre-requisites

The full version of Adobe Acrobat (the free reader does not expose the COM interfaces) which exposes a COM library of objects to manipulate and access PDF information.
The Adobe Acrobat 5.0 SDK which is a free download from the Adobe Solutions Network website (note: the site requires registration). The latest SDK for Acrobat 10.0 requires paid membership, so we will use the previous SDK version. 


How Thumbnail View of PDF Generated Programmatically
·        In this code I used Acrobat for dll to generate Thumbnail view of PDF
Acrobat.CAcroPDDoc pdfDoc;
Acrobat.CAcroPDPage pdfPage;
Acrobat.CAcroRect pdfRect;
Acrobat.CAcroPoint pdfPoint;

·        Read PDF
Type AcrobatPDDocType;
AcrobatPDDocType = Type.GetTypeFromProgID("AcroExch.PDDoc");
pdfDoc = (Acrobat.CAcroPDDoc)Activator.CreateInstance(AcrobatPDDocType);

pdfDoc.Open(pdfpath);

·        Get first page of PDF
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(0);


·        Create Rectangle size for generating PDF Image 
        pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
Type AcrobatpdfRectType
AcrobatpdfRectType = Type.GetTypeFromProgID("AcroExch.Rect");          
 pdfRect = (Acrobat.CAcroRect)Activator.CreateInstance(AcrobatpdfRectType);

pdfRect.Left = 0; 
pdfRect.right = pdfPoint.x;
pdfRect.Top = 0;
pdfRect.bottom = pdfPoint.y;

·        Copy the PDF Image To Clipboard
 pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);


·        Get the Image from Clipboard using Thread(For Windows application no need of thread)
 Thread cbThread = new Thread(new ThreadStart(CopyToClipboard));
 cbThread.ApartmentState = ApartmentState.STA;
 cbThread.Start();
 cbThread.Join();
       
 Function:            
 [STAThread]
 protected void CopyToClipboard()
 {
            IDataObject data = Clipboard.GetDataObject();
            pdfBitmap = (System.Drawing.Bitmap)data.GetData(DataFormats.Bitmap);

 }

·        Finally Create Thumbnail Image of PDF in specified folder
 Image pdfImage = pdfBitmap.GetThumbnailImage(thumbnailWidth, thumbnailHeight,
                                                         null, IntPtr.Zero);
 Bitmap thumbnailBitmap = new Bitmap(thumbnailWidth + 7, thumbnailHeight + 7,
                                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);


templateBitmap.MakeTransparent();
       
using (Graphics thumbnailGraphics = Graphics.FromImage(thumbnailBitmap))
{
  // Draw rendered pdf image to new blank bitmap
   thumbnailGraphics.DrawImage(pdfImage, 2, 2, thumbnailWidth, thumbnailHeight);

  // Save as .png file
  thumbnailBitmap.Save(OutputImagePath, System.Drawing.Imaging.ImageFormat.Png);

}

pdfDoc.Close();

// Not sure how why it is to do this, but Acrobat is not the best behaved COM object
// see http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);

·        Now we can use the OutputImagePath for thumbnail view of Particular PDF

No comments:

Post a Comment