Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

adildacoolset

macrumors 65816
Original poster
Hello!

I'm trying to display a pdf file in my app, using core graphics. I made a custom view, with the relevant code here for the view (got some lead from the Quartz 2D Programming Guide):

Code:
- (CGPDFDocumentRef)myGetDocumentRef:(const char *)fileName {

    CFStringRef path;
    CFURLRef url;
    CGPDFDocumentRef document;
    size_t count;
    
    path = CFStringCreateWithCString(NULL, fileName, kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
    
    CFRelease(path);
    document = CGPDFDocumentCreateWithURL(url);
    
    if(!document) {
        
        NSLog(@"Failed to create document!");
    }
    CFRelease(url);
    count = CGPDFDocumentGetNumberOfPages(document);
    
    return document;
    
}


- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGPDFDocumentRef document;
    CGPDFPageRef page;
    
    document = [self myGetDocumentRef:"DocumentName.pdf"];
    page = CGPDFDocumentGetPage(document, 1);
    CGContextDrawPDFPage(context, page);
    CGPDFDocumentRelease(document);
}

This, for some reason, doesn't display a pdf file. I had investigated a bit with breakpoints, and it looks like the document variable was null. To confirm my suspicion, the NSLog gets triggered.

And I made sure that the pdf file name is the exact same as the name as the pdf file I added to Xcode.

Please someone help me.
 

adildacoolset

macrumors 65816
Original poster
UIWebView displays pdf files pretty nicely.

Also see

https://github.com/vfr/Reader

Thanks for the time taken to post, and sorry for the late reply!

As it stands, I tried your advice and did a UIWebView. The problem is that it displays shadows. I'm going to do page-turns using a page view controller, so shadows won't look good in that case.

Anyways, I managed to sort it out using guidance from here:http://iosguy.com/2010/09/04/presenting-pdf-files-by-yourself/ and I'm still using core graphics. Here is the modified code (I only included the function which was modified. The drawRect is unchanged):

Code:
- (CGPDFDocumentRef)myGetDocumentRef:(const char *)fileName {

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"File" ofType:@"pdf"];
    NSURL *url = [[NSURL alloc]initFileURLWithPath:filePath];
    CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);
    return document;
    
}

Thanks for taking your time!
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Glad you got it working. Yeah those CF functions can be a pain to use. If I was doing a lot of work with them I would probably write some wrapper classes for the PDFDocument and PDFPage to avoid having the memory management code and __bridge casts everywhere in my nice Objective-C code.
 

adildacoolset

macrumors 65816
Original poster
Glad you got it working. Yeah those CF functions can be a pain to use. If I was doing a lot of work with them I would probably write some wrapper classes for the PDFDocument and PDFPage to avoid having the memory management code and __bridge casts everywhere in my nice Objective-C code.

Yeah, thanks for that. I'll be sure to try that, as I like having tidy code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.