Below is the code I wrote and tested on tvOS. Note that this is in Objective-c.
I created two functions for completing the task and one helper function for displaying PDF images in UIScrollView. The first will open the PDF from the URL. The web address was used. You can also use a local file in this example.
There is also a helper function to open a document from a local file.
The second function turns a PDF document into context. I decided to display the context by creating an image from it. There are other ways to handle context.
Opening a document is pretty straight forward, so there are no comments in the code for this. Rendering a document is somewhat more active, so there are comments explaining this feature.
Below is the full application.
- (CGPDFDocumentRef)openPDFLocal:(NSString *)pdfURL { NSURL* NSUrl = [NSURL fileURLWithPath:pdfURL]; return [self openPDF:NSUrl]; } - (CGPDFDocumentRef)openPDFURL:(NSString *)pdfURL { NSURL* NSUrl= [NSURL URLWithString:pdfURL]; return [self openPDF:NSUrl]; } - (CGPDFDocumentRef)openPDF:(NSURL*)NSUrl { CFURLRef url = (CFURLRef)CFBridgingRetain(NSUrl); CGPDFDocumentRef myDocument; myDocument = CGPDFDocumentCreateWithURL(url); if (myDocument == NULL) { NSLog(@"can't open %@", NSUrl); CFRelease (url); return nil; } CFRelease (url); if (CGPDFDocumentGetNumberOfPages(myDocument) == 0) { CGPDFDocumentRelease(myDocument); return nil; } return myDocument; } - (void)drawDocument:(CGPDFDocumentRef)pdfDocument {
And to tie it all together, you can do this:
CGPDFDocumentRef pdfDocument = [self openPDFURL:@"http://www.guardiansuk.com/uploads/accreditation/10testing.pdf"]; [self drawDocument:pdfDocument];
Please note that I am using a random PDF file that was available for free on the Internet. I ran into some problems with https urls, but I'm sure this can be resolved, and this is not actually related to the issue of opening a PDF.
Vel genov
source share