Knowledge Base - How to create a thumbnail from a page (iOS)

This article shows how to create a thumbnail of a required page.

- (CGImageRef)getThumbnailFromPage:(NSString *)m_path password:(NSString *)password page:(int)pageno width:(int)thumbWidth height:(int)thumbHeight

{

    PDFDoc *doc = [[PDFDoc alloc] init];

    

    if ([doc open:m_path :password] == 0) {

        PDFPage *page = [doc page:pageno];

        

        PDF_DIB m_dib = NULL;

        PDF_DIB bmp = Global_dibGet(m_dib, thumbWidth, thumbHeight);

        

        float w = [doc pageWidth:pageno];

        float h = [doc pageHeight:pageno];

        float ratiox = thumbWidth/w;

        float ratioy = thumbHeight/h;

        if( ratiox > ratioy ) ratiox = ratioy;

        

        PDF_MATRIX mat = Matrix_createScale(ratiox, -ratiox, 0, h * ratioy);

        Page_renderPrepare(page.handle, bmp);

        Page_render(page.handle, bmp, mat, false, 1);

        Matrix_destroy(mat);

        page = nil;

        

        void *data = Global_dibGetData(bmp);

        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, thumbWidth * thumbHeight * 4, NULL);

        CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();

        CGImageRef imgRef = CGImageCreate(thumbWidth, thumbHeight, 8, 32, thumbWidth<<2, cs, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst, provider, NULL, FALSE, kCGRenderingIntentDefault);

        

        CGContextRef context = CGBitmapContextCreate(NULL, thumbWidth, thumbHeight, 8, 0, cs, kCGImageAlphaPremultipliedLast);

        

        // Draw ...

        CGContextSetAlpha(context, 1);

        CGContextSetRGBFillColor(context, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)1.0 );

        CGContextDrawImage(context, CGRectMake(0, 0, thumbWidth, thumbHeight), imgRef);

        

        // Get your image

        CGImageRef cgImage = CGBitmapContextCreateImage(context);

        

        // Release

        CGColorSpaceRelease(cs);

        CGDataProviderRelease(provider);

        

        page = nil;

        doc = nil;

                

        return cgImage;

    }

    

    return nil;

 

}

Applies To

RadaeePDF SDK for iOS

Details

Created : 2017-10-19 10:40:05, Last Modified : 2017-10-19 10:40:05