4

Currently i am working in iPhone application, i have an pdf file in resource folder (Local pdf file) then i read that pdf file (paper.pdf) successfully, below i have mentioned read local pdf file for your reference.

Example:

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

Then i have tried to store pdf file (from URL) in NSDocument directory, stored 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 read that pdf file (from NSDocument directory) but i didn't know that, please help me.

Thanks in Advance

SampathKumar
  • 2,525
  • 8
  • 47
  • 82

2 Answers2

9

its very simple please use below code to display your pdf from document directory to Webview

load PDF from Document Directory

 - (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];
 }

load PDF from Application Bundle 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];
 }
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
8

Try to use 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 NSURL reference:

The NSURL class is toll-free bridged with its Core Foundation counterpart, CFURLRef. For more information on toll-free bridging, see “Toll-Free Bridging”.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • @sergio i'm dowloading my pdf file with the timestamp, something like this: /Documents/myPDF.pdf1382698338.78128 so now how can i appendpathComponent ?? – zeeshan shaikh Oct 25 '13 at 11:00
  • @zeeshanshaikh: this makes for another question with more details about what you are doing... – sergio Oct 25 '13 at 11:06
  • @sergio please check my question: http://stackoverflow.com/questions/19588173/how-to-read-pdf-file-from-document-directory-with-timestamp-in-iphone – zeeshan shaikh Oct 25 '13 at 11:13
  • @sergio please check my comment on that question which you have answered thanks – zeeshan shaikh Oct 25 '13 at 12:08