Wednesday, October 24, 2012

iOS - Getting thumbnail of PDF document

We can get the thumbnail of any page of pdf document. The following example is getting the thumbnail of first page

            NSURL* pdfFileUrl = [NSURL fileURLWithPath:filePath];
            CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);
           
            CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);//for the first  page
            CGRect aRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);           
            UIGraphicsBeginImageContext(aRect.size);
            CGContextRef context = UIGraphicsGetCurrentContext();
           
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, 0.0, aRect.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextTranslateCTM(context, -(aRect.origin.x), -(aRect.origin.y));
           
            CGContextSetGrayFillColor(context, 1.0, 1.0);
            CGContextFillRect(context, aRect);
           
            CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, aRect, 0, false);
            CGContextConcatCTM(context, pdfTransform);
            CGContextDrawPDFPage(context, page);
           
            UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();           
            CGContextRestoreGState(context);
            UIGraphicsEndImageContext();
            CGPDFDocumentRelease(pdf);

2 comments:

  1. Thanks for this code. It will surely help iOS developers to use in their PDF thumbnail related programming works. We as Techcronus are iOS mobile app developer company and having expert iOS developers to transform your idea into a mobile app. Connect with us here for more information: Techcronus iOS development

    ReplyDelete