How to read a PDF file from the document catalog in iPhone?

I am currently working in an iPhone application, I have a pdf file in the resources folder (local pdf file), after which I read this pdf file (paper.pdf) successfully, below I mentioned reading the local pdf file for reference.

Example:

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("paper.pdf"), NULL, NULL); pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); CFRelease(pdfURL); 

Then I tried to save the pdf file (from the URL) in the NSDocument directory, saved successfully.

 NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.msy.com.au/Parts/PARTS.pdf"]]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myPDF11.pdf"]; [pdfData writeToFile:filePath atomically:YES]; 

Then I tried to read this pdf file (from the NSDocument directory), but I did not know this, please help me.

Thanks at Advance

+4
source share
2 answers

Try using this:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myPDF11.pdf"]; NSURL *pdfUrl = [NSURL fileURLWithPath:filePath]; pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); CFRelease(pdfURL); 

From the NSURL link :

The NSURL class seamlessly connects with its Core Foundation counterpart, CFURLRef. For more information on the free bridge connection, see "Bridge Connection".

+7
source

its very simple, please use the code below to display a pdf file from the document directory in Webview

Download PDF from the document catalog

  - (void)viewDidLoad { [super viewDidLoad]; UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)]; webView.scalesPageToFit = YES; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myPDF11.pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:filePath]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [webView loadRequest:request]; [self.view addSubview:webView]; [webView release]; } 

Download PDF from the Bundle App Resource Folder

  - (void)viewDidLoad { [super viewDidLoad]; UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)]; webView.scalesPageToFit = YES; NSString *path = [[NSBundle mainBundle] pathForResource:@"myPDF11" ofType:@"pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [webView loadRequest:request]; [self.view addSubview:webView]; [webView release]; } 
+9
source

Source: https://habr.com/ru/post/1437556/


All Articles