2013-03-19 62 views
-1

我需要在大型hq PDF文件中嵌入縮略圖,這些文件是使用UIGraphicsBeginPDFContextToFile和UIGraphicsBeginPDFPage創建的,並在其上繪製文本和圖像。iOS在PDF中嵌入頁面縮略圖

有誰知道,如何嵌入頁面大拇指?

的Ciao,阿諾

回答

0

如果你想從PDF生成縮略圖,那麼你可以使用下面的代碼。該代碼將其寫入磁盤。很容易改變它,以便該方法返回圖像。

- (void)createPDFThumbnailForFile:(NSString *)theFilename { 
    if (!theFilename) {return;} 
    @try { 
     NSString *path = [FileInfo fullPathForFile:theFilename]; 
     NSURL *pdfFileUrl = [NSURL fileURLWithPath:path]; 
     CFURLRef pdfFileRef = (__bridge CFURLRef) pdfFileUrl; 
     CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfFileRef); 
     CGPDFPageRef page; 
     CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size 
     UIGraphicsBeginImageContext(aRect.size); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     UIImage *thumbnailImage; 
    // NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf); 
     //we only want the first page 
     for (int i = 0; i < 1; 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, 1); 
      CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, aRect, 0, true); 
      // And apply the transform. 
      CGContextConcatCTM(context, pdfTransform); 

      CGContextDrawPDFPage(context, page); 

      // Create the new UIImage from the context 
      thumbnailImage = UIGraphicsGetImageFromCurrentImageContext(); 
      CGContextRestoreGState(context); 
     } 
     CGPDFDocumentRelease(pdf); 
     NSString *pngPath = [path stringByReplacingOccurrencesOfString:@".pdf" withString:@".png"]; 
    // [@"test" writeToFile:pngPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; 
     [UIImagePNGRepresentation(thumbnailImage) writeToFile:pngPath atomically:YES]; 
    } 
    @catch (NSException *exception) { 
     DebugLog(@"Could not write thumbnail to : %@ /n --> %@", theFileToSave, exception.description); 
    } 

} 
+0

對不起,我知道如何繪製*大拇指,但我需要將它們作爲頁面縮略圖嵌入pdf中。 – akw 2013-03-20 00:07:06