PDF file is not displaying

Discussion in 'iOS Programming' started by adildacoolset, May 20, 2013.

  1. adildacoolset macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #1
    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.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  3. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #3
    UIDocumentInteractionController is very handy in these situations as well.
     
  4. adildacoolset thread starter macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #4
    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!
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    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.
     
  6. adildacoolset thread starter macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #6
    Yeah, thanks for that. I'll be sure to try that, as I like having tidy code.
     

Share This Page