Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is the first PDF class I have made for the iOS using code from Apple's samples, there could easily be stuff here wrong that I am missing.

I intend this to get images out of for a layer, for example:

image.contents = (id)[self.test imageForPage: 3 size: CGSizeMake(4*dpc, 2*dpc)].CGImage;

Here is this hopefully decent code, any suggestions are welcome...

@interface PDFDocument : NSObject {
CGPDFDocumentRef pdfFile;
}

- (id) initWithURL: (NSURL*) url;
- (UIImage*) imageForPage: (size_t) pageno size: (CGSize) size;
- (CGSize) sizeOfPage: (size_t) pageno;

@end

@implementation PDFDocument

- (id) initWithURL: (NSURL*) url {
    if ([super init])
    {
        // Open PDF
        CGPDFDocumentRef doc = CGPDFDocumentCreateWithURL((CFURLRef)url);

        if (doc == NULL)
            @throw @"PDF File does not exist.";

        pdfFile = doc;

    }
    return self;
}

- (UIImage*) imageForPage: (size_t) pageno size: (CGSize) size {
    if (pdfFile) {
        // Get First Page
        CGPDFPageRef page = CGPDFDocumentGetPage(pdfFile, pageno);

        if (page == NULL)
            @throw @"Page does not exist.";

        // Get Page Size
        CGRect cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);

        // Start Drawing Context to Render PDF
        UIGraphicsBeginImageContext(size);

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);

        CGRect u = { {0, 0}, size};

        CGContextFillRect(context, u);
        CGContextSetAllowsAntialiasing(context, NO);

        CGContextSaveGState(context);

        // Scale PDF
        CGContextScaleCTM(context, size.width / cropBox.size.width, size.height / cropBox.size.height);

        // Flip Context to render PDF correctly
        CGContextTranslateCTM(context, 0.0, cropBox.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        CGContextDrawPDFPage(context, page);
        CGContextRestoreGState(context);

        UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); // must retain?

        UIGraphicsEndImageContext();

        return img;

    }
    return nil;
}

- (CGSize) sizeOfPage: (size_t) pageno {
    if (pdfFile) {
        CGPDFPageRef page = CGPDFDocumentGetPage(pdfFile, pageno);

        if (page == NULL)
            @throw @"Page does not exist";

        CGRect cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);

        return cropBox.size;
    }

    @throw @"No document loaded.";
}

@end
share|improve this question
I have encountered PDF files where the text is written in landscape but the page width and height will tell you that the page is in portrait. When opened in a PDF viewer, the file will be in landscape. I had to add special handling for files like those. Unfortunately, I don't have my code with me right now so I can't share, but I believe you're missing that part. – Altealice May 31 '11 at 5:35
Something like this? ipdfdev.com/2011/03/23/… – drakej May 31 '11 at 13:35
Yeah, something like that. But I remember mine to be much simpler, like ~8 additional lines only. – Altealice Jun 1 '11 at 13:29

migrated from stackoverflow.com May 31 '11 at 1:47

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.