【问题标题】:How to generate thumbnails from a PDF document with iPhone SDK?如何使用 iPhone SDK 从 PDF 文档生成缩略图?
【发布时间】:2010-12-21 23:11:36
【问题描述】:

我已阅读the Apple PDF documentation with Quartz

但我不知道如何从 PDF 文档生成缩略图...

任何想法/示例代码?

【问题讨论】:

    标签: iphone pdf quartz-graphics


    【解决方案1】:

    这是我的示例代码。

    NSURL* pdfFileUrl = [NSURL fileURLWithPath:finalPath];
    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);
    CGPDFPageRef page;
    
    CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
    UIGraphicsBeginImageContext(aRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage* thumbnailImage;
    
    
    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, kCGPDFMediaBox, aRect, 0, true);
        // And apply the transform.
        CGContextConcatCTM(context, pdfTransform);
    
        CGContextDrawPDFPage(context, page);
    
        // Create the new UIImage from the context
        thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
    
        //Use thumbnailImage (e.g. drawing, saving it to a file, etc)
    
        CGContextRestoreGState(context);
    
    }
    
    
    UIGraphicsEndImageContext();    
    CGPDFDocumentRelease(pdf);
    

    【讨论】:

    • 欢迎:请选择我的答案:)。对于您的信息,如果您尝试在 iPad 上制作超过 300dpi、有数十页的 PDF 的所有缩略图,您可能会遇到崩溃。
    • 这给出了缩略图的最后一页图像,我删除了 for 循环,我得到了 pdf 的第一页,无论如何你的回答对我有很大帮助。
    【解决方案2】:

    从 iOS 11 你可以使用PDFKit

    import PDFKit
    
    func generatePdfThumbnail(of thumbnailSize: CGSize , for documentUrl: URL, atPage pageIndex: Int) -> UIImage? {
        let pdfDocument = PDFDocument(url: documentUrl)
        let pdfDocumentPage = pdfDocument?.page(at: pageIndex)
        return pdfDocumentPage?.thumbnail(of: thumbnailSize, for: PDFDisplayBox.trimBox)
    }
    

    叫它:

    let thumbnailSize = CGSize(width: 100, height: 100)
    
    let thumbnail = generatePdfThumbnail(of: thumbnailSize, for: url, atPage: 0)
    

    【讨论】:

      【解决方案3】:

      从可以使用 PDFKit 框架的 iOS 11 开始,您可以像这样简单地获得缩略图:

      import PDFKit
      
      func pdfThumbnail(url: URL, width: CGFloat = 240) -> UIImage? {
        guard let data = try? Data(contentsOf: url),
        let page = PDFDocument(data: data)?.page(at: 0) else {
          return nil
        }
      
        let pageSize = page.bounds(for: .mediaBox)
        let pdfScale = width / pageSize.width
      
        // Apply if you're displaying the thumbnail on screen
        let scale = UIScreen.main.scale * pdfScale
        let screenSize = CGSize(width: pageSize.width * scale, 
                                height: pageSize.height * scale)
      
        return page.thumbnail(of: screenSize, for: .mediaBox)
      } 
      

      【讨论】:

      • 我使用此代码加载本地和远程位置 pdf 缩略图工作正常,但导航屏幕至少需要 5 秒
      • 任何帮助@Max Desiatov
      • 在不了解更多细节的情况下很难说出为什么会这样。 PDF的大小是多少?这可以在所有 iOS 版本和设备上重现还是仅在模拟器上重现?你使用哪个版本的 Xcode?即使 PDF 很小,它仍然可能是一个复杂的需要很长时间才能呈现的文件,因此附加该 PDF 可能最适合诊断问题
      • 我正在使用 Xcode 10.3 swift4.2 并且 pdf 文件大小小于 3mb,并且它可以在所有 ios 设备和模拟器上重现 @Max Desiatov
      • 能否附上 PDF 以便其他人可以复制它?
      【解决方案4】:

      与此同时,我创建了一个库,它支持 PDF、图像和视频来创建缩略图。也许其他人可以使用它:

      https://github.com/prine/ROThumbnailGenerator

      Swift 中的解决方案。只需将 NSURL 传递给 PDF 和要检索为 UIImage 的页码:

      func getThumbnail(url:NSURL, pageNumber:Int) -> UIImage {
      
          var pdf:CGPDFDocumentRef = CGPDFDocumentCreateWithURL(url as CFURLRef);
      
          var firstPage = CGPDFDocumentGetPage(pdf, pageNumber)
      
          // Change the width of the thumbnail here
          var width:CGFloat = 240.0;
      
          var pageRect:CGRect = CGPDFPageGetBoxRect(firstPage, kCGPDFMediaBox);
          var pdfScale:CGFloat = width/pageRect.size.width;
          pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);
          pageRect.origin = CGPointZero;
      
          UIGraphicsBeginImageContext(pageRect.size);
      
          var context:CGContextRef = UIGraphicsGetCurrentContext();
      
          // White BG
          CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
          CGContextFillRect(context,pageRect);
      
          CGContextSaveGState(context);
      
          // ***********
          // Next 3 lines makes the rotations so that the page look in the right direction
          // ***********
          CGContextTranslateCTM(context, 0.0, pageRect.size.height);
          CGContextScaleCTM(context, 1.0, -1.0);
          CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(firstPage, kCGPDFMediaBox, pageRect, 0, true));
      
          CGContextDrawPDFPage(context, firstPage);
          CGContextRestoreGState(context);
      
          var thm:UIImage = UIGraphicsGetImageFromCurrentImageContext();
      
          UIGraphicsEndImageContext();
          return thm;
      }
      

      【讨论】:

        【解决方案5】:

        Swift 4 Prine 的答案版本

        func thumbnailFromPdf(withUrl url:URL, pageNumber:Int, width: CGFloat = 240) -> UIImage? {
            guard let pdf = CGPDFDocument(url as CFURL),
                let page = pdf.page(at: pageNumber)
                else {
                    return nil
            }
        
            var pageRect = page.getBoxRect(.mediaBox)
            let pdfScale = width / pageRect.size.width
            pageRect.size = CGSize(width: pageRect.size.width*pdfScale, height: pageRect.size.height*pdfScale)
            pageRect.origin = .zero
        
            UIGraphicsBeginImageContext(pageRect.size)
            let context = UIGraphicsGetCurrentContext()!
        
            // White BG
            context.setFillColor(UIColor.white.cgColor)
            context.fill(pageRect)
            context.saveGState()
        
            // Next 3 lines makes the rotations so that the page look in the right direction
            context.translateBy(x: 0.0, y: pageRect.size.height)
            context.scaleBy(x: 1.0, y: -1.0)
            context.concatenate(page.getDrawingTransform(.mediaBox, rect: pageRect, rotate: 0, preserveAspectRatio: true))
        
            context.drawPDFPage(page)
            context.restoreGState()
        
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
        

        【讨论】:

          【解决方案6】:

          对于objective-c

          CGRect pageSize = [[self.pdfView.document pageAtIndex:0] boundsForBox:kPDFDisplayBoxMediaBox];
          CGFloat pdfScale = [UIScreen mainScreen].bounds.size.width /pageSize.size.width;
          CGFloat scale = [UIScreen mainScreen].scale * pdfScale;
          UIImage thumbnailImage = [[self.pdfView.document pageAtIndex:0] thumbnailOfSize:CGSizeMake(pageSize.size.width *scale, pageSize.size.height *scale) forBox:kPDFDisplayBoxMediaBox];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-11-29
            • 2017-11-16
            • 2010-11-03
            • 2011-04-27
            • 2011-09-15
            • 2021-01-01
            • 1970-01-01
            • 2011-11-28
            相关资源
            最近更新 更多