【问题标题】:iOS embedd page thumbnails in PDFiOS 在 PDF 中嵌入页面缩略图
【发布时间】:2013-03-08 13:42:18
【问题描述】:

我需要在大型 hq PDF 文件中嵌入缩略图,我使用 UIGraphicsBeginPDFContextToFile 和 UIGraphicsBeginPDFPage 创建并在其上绘制文本和图像。

有谁知道,如何嵌入页面缩略图?

Ciao,阿诺

【问题讨论】:

    标签: ios pdf thumbnails


    【解决方案1】:

    如果您想从 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);
        }
    
    }
    

    【讨论】:

    • 抱歉,我知道如何绘制缩略图,但我需要将它们作为页面缩略图嵌入到 pdf 中。
    猜你喜欢
    • 2011-11-28
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    相关资源
    最近更新 更多