Hello I'm developing an application which displays a pdf file in UIWebView
. I want to display the thumbnails of all the pages of pdf files it contains where the user can select his own page he is interested in. I found one sample code to get thumbnails but I'm unable to display it.
UIWebView* webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor whiteColor];
NSString *fileName= @"2_slo_sample";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pdf"];
NSURL* pdfFileUrl = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
webView.scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 480);
webView.scrollView.alwaysBounceHorizontal = YES;
[self.view addSubview:webView];
CGPDFPageRef page;
CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
UIGraphicsBeginImageContext(aRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfFileUrl);
NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);
for(int i = 0; i < totalNum; i++ ) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, aRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetGrayFillColor(context, 1.0, 1.0);
CGContextFillRect(context, aRect);
// Grab the first PDF page
page = CGPDFDocumentGetPage(pdf, i + 1);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, aRect, 0, false);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
// Create the new UIImage from the context
thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
}
UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdf);
I'm sure I'm masking some mistake in it. Could you please tell me where I need to get corrected?
just above the for loop
int x, y, w, h;
x = 5;
y = 5;
now in :
for(int i = 0; i < totalNum; i++ ) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, aRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetGrayFillColor(context, 1.0, 1.0);
CGContextFillRect(context, aRect);
// Grab the first PDF page
page = CGPDFDocumentGetPage(pdf, i + 1);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, aRect, 0, false);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
// Create the new UIImage from the context
thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
**[self drawImage:thumbnailImage withX:x withY:y];**
**x = x +100;**
CGContextRestoreGState(context);
}
and method definition as :
-(void)drawImage:(UIImage *)image withX:(int)x withY:(int)y
{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame =CGRectMake(x, y, **image.size.width**, **image.size.height**);
imageView.image = image;
[self.webView addSubview:imageView];
}
ideally you should add pdf thumbnail in view/not in web view (if requirement then its ok) hope this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.